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 of Java?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you about the internal classes of 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.

An inner class refers to defining another class inside an external class. The class name does not need to be the same as the folder.

The inner class is divided into member inner class, local inner class, static nested class and anonymous inner class.

1. Member inner class

The member inner class is the most common inner class, which is defined as inside another class and looks like the following:

Class Outter {private int age = 12; class Inner {private int age = 13; public void print () {int age = 14; System.out.println ("local variables:" + age); System.out.println ("inner class variables:" + this.age); System.out.println ("external class variables:" + Out.this.age) } public class test1 {public static void main (String [] args) {Outter out = new Outter (); Outter.Inner in = out.new Inner (); in.print ();}}

Running result:

Local variable: 14

Inner class variable: 13

External class variable: 12

You can see from this example that the inner class of a member, as a member of an external class, can use all the members and methods of the external class directly, even if it is private. Although the member inner class can unconditionally access the members of the external class, the external class wants to access the members of the member inner class is not so arbitrary. If you want to access a member of a member's inner class in an external class, you must first create an object of the member's inner class, and then access it through a reference to that object:

Class Outter {private int age = 12; public Outter (int age) {this.age = age; getInInstance (). Print (); / / you must create an object of the inner class of the member before accessing!} private Inner getInInstance () {return new Inner () } class Inner {public void print () {System.out.println ("the inner class does not have the same name, so call the external class member variable directly:" + age);} public class test1 {public static void main (String [] args) {Outter out = new Outter (10);}}

Running result:

The inner class does not have the same name, so the external class member variable is called directly: 10

Inner classes can have private access, protected access, public access, and package access.

For example, in the above example, if the member inner class Inner is decorated with private, it can only be accessed inside the external class. If it is decorated with public, it can be accessed anywhere; if it is decorated with protected, it can only be accessed under the same package or if it inherits the external class; if it is the default access permission, it can only be accessed under the same package.

This is a little different from external classes, which can only be decorated with public and package access permissions.

Personally, I understand that because the inner class of a member looks like a member of an external class, it can have a variety of permission modifications like members of the class. Note that the inner class of a member cannot contain variables and methods of static. Because the inner class of a member needs to create an external class before it can create its own

two。 Local inner class

A local inner class is a class defined in a method or scope, which differs from the member inner class in that the access to the local inner class is limited to the method or scope.

The inner class defined in the method:

Class Outter {private int age = 12; public void Print (final int x) {/ / where the local variable x must be set to type final! Class Inner {public void inPrint () {System.out.println (x); System.out.println (age);}} new Inner (). InPrint ();}} public class test1 {public static void main (String [] args) {Outter out = new Outter (); out.Print (10);}}

Running result:

ten

twelve

In this case, we move the inner class to the method of the outer class, and then regenerate it into an inner class object in the method of the outer class to call the inner class method. If we need to pass parameters to the methods of the external class at this time, then the method formal parameters of the external class must be defined using final.

In other words, the inner class defined in the method can only access the local variable of type final in the method, because the local variable defined in the method is equivalent to a constant, its life cycle exceeds the life cycle of the method line, and because the local variable is set to final, you can no longer change the value of the local variable in the inner class. (here we can see that there are different explanations on the Internet, which have not been thoroughly understood.)

Define inner classes within scope:

Class Outter {private int age = 12; public void Print (final boolean x) {/ / where the local variable x must be set to type final! If (x) {class Inner {public void inPrint () {System.out.println (age);}} new Inner (). InPrint ();} public class test1 {public static void main (String [] args) {Outter out = new Outter () Out.Print (true);}}

Running result: 12

3. Static nested class

Also known as static local class, nested inner class, is modified as static inner class. The inner class declared as static does not need the connection between the inner class object and the external class object, which means that we can refer to outer.inner directly, that is, we do not need to create an external class or an inner class.

Class Outter {private static int age = 12; static class Inner {public void print () {System.out.println (age);} public class test1 {public static void main (String [] args) {Outter.Inner in = new Outter.Inner (); in.print ();}}

Running result:

twelve

As you can see, if the inner is static with static, then the inner class can only access the static member variables of the external class, which is limited.

Second, because the inner class is static, Outter.Inner can be viewed as a whole and new the objects of the inner class directly (access static through the class name, it doesn't matter whether the external class object is generated or not)

4. Anonymous inner class

Anonymous inner classes should be the ones we use most when writing code. Using anonymous inner classes when writing code for event monitoring is not only convenient, but also makes the code easier to maintain. The following code is a piece of Android event listening code:

Scan_bt.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {/ / TODO Auto-generated method stub}}) History_bt.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {/ / TODO Auto-generated method stub}})

This code sets listeners for two buttons, which uses anonymous inner classes. The specific location is this section:

New OnClickListener () {@ Override public void onClick (View v) {/ / TODO Auto-generated method stub}}

In the code, you need to set a listener object for the button, and using anonymous inner classes can generate a corresponding object while implementing the methods in the parent class or interface, but only if the parent class or interface exists before it can be used in this way. Of course, it is possible to write something like the following, which is the same as using anonymous inner classes above:

Private void setListener () {scan_bt.setOnClickListener (new Listener1 ()); history_bt.setOnClickListener (new Listener2 ());} class Listener1 implements View.OnClickListener {@ Override public void onClick (View v) {/ / TODO Auto-generated method stub}} class Listener2 implements View.OnClickListener {@ Override public void onClick (View v) {/ / TODO Auto-generated method stub}}

Although this writing method can achieve the same effect, it is lengthy and difficult to maintain, so it generally uses the method of anonymous inner class to write event listening code. Similarly, anonymous inner classes cannot have access modifiers and static modifiers.

An anonymous inner class is a class without a constructor. Because it has no constructor, the scope of use of anonymous inner classes is very limited, and most anonymous inner classes are used for interface callbacks. The anonymous inner class is automatically named Outter$1.class by the system at compile time. In general, anonymous inner classes are used to inherit other classes or implement interfaces without adding additional methods, just the implementation or rewriting of inherited methods.

These are the internal classes of 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

Development

Wechat

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

12
Report