Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the inner classes in Java

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about the internal classes in Java. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Why use inner classes?

Each inner class in    can inherit from an implementation independently, so whether or not the external class inherits an implementation has no effect on the inner class.

Member inner class

Why? Because static variables and methods do not depend on objects, they are only related to classes. When loading a static domain, there are no external classes at all. Therefore, static variables and methods cannot be defined in non-static inner classes, and the compilation will fail.

An inner class is equivalent to a member variable for an external class. Inner classes can use any access control character

Methods of an inner class can directly access the data of an external class without being affected by access control characters

Create an inner class object method: an outer class. Inner class object name = external class object .new inner class ()

After compilation, two class files are produced: Outer.class and Outer$Inner.class

There cannot be any static variables and methods in the inner class

Static inner class: inner class decorated with static

Static inner class accesses non-static members of external class: not directly, new external class () is required. Members

If you access the static member of the external class (the name is the same as the inner class): "class name. static member"

If you access the static member of an external class (whose name is different from the inner class): "static member name"

Create static inner class objects (no external class objects are required): external classes. Inner class object name = new external class. Inner class ()

Method inner class: access is limited to within the method (or within the scope)

A local inner class, like a local variable in a method, cannot be modified with public protected private static

Only local variables of type final defined in the method can be accessed. Reason: local variables die as the method body finishes. But the inner class object still exists. (causes the inner class to access a local variable that does not exist) (using final not only maintains the reference of the object, but also allows the compiler to maintain the life cycle of the object in the callback method) (the local inner class is not the parameter passed in by directly calling the method, but the inner class backs up the parameter through the constructor)

Anonymous inner class

Anonymous inner classes directly use new to generate a reference to an object

Disadvantages: can only be used once

Using anonymous inner classes must inherit a class or implement an interface (you can't have both)

Anonymous inner classes cannot have constructors, nor can there be any static member variables and static methods

An anonymous inner class cannot be abstract and must implement all abstract methods of an inherited class or an implemented interface

Anonymous internal column initialization: use the construction code block. Using construction code blocks can achieve the effect of creating a constructor for anonymous inner classes.

These are the internal classes in the Java shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report