Java internal classes
In Java programming language, program is built by class. A class can also be declared inside a class. We call such a class an inner class. The definition of a simple inner class is as follows:
Why use inner classes
The most attractive reason for using inner classes is that each inner class can independently inherit an implementation (Interface), so no matter whether the outer class has inherited an implementation (Interface), it has no effect on the inner class. That is to say, the inner class has the basic characteristics of the class and can inherit the parent class to implement the interface
/** 1. Outer Class inherits ClassA and implements IFunctionA */ public class Outer extends ClassA implements IFunctionA < /** * Inner Class inherits ClassB and implements IFunctionB */ public class Inner extends ClassB implements IfunctionB< // >>
Inner class properties
The biggest advantage of using inner class is that it can solve the problem of multiple inheritance. Using inner class can also bring us the following features
- Internal class can use multiple instances, each of which has its own state information and is independent of the information of other external objects.
- In a single external class, multiple internal classes can implement the same interface or inherit the same class in different ways.
- The creation time of the peripheral object does not depend on the creation time of the inner class of the object
- The inner class has no confusing «is-a» relationship, it is an independent entity.
- The inner class provides a better encapsulation, and no other class can access it except the outer class.
In summary, the inner class is more like part of the outer class.
Principles of using internal class
The principles of using internal and external classes are as follows:
- External classes can only use public access modifiers, but internal classes can use all access modifiers, such as public, protected, private, etc
- Class usage: the external class can be directly instantiated, and there is nothing about the internal class in the instantiated external class. Basically, the internal class can be ignored when using the external class; the external class object needs to be instantiated before using the internal class, that is, the internal class object name = external class object. new internal class ();
- The inner class can directly access and call the fields of the outer class, but the field methods inside the inner class are given priority. Execution order: method of this internal class, method outside, and external class. If the external class and the internal class have the same member variable or method, the internal class will access its own member variable or method by default. If you want to access the member variable of the external class, you can use this keyword, such as: Outer.this.name
- All fields of the external class, whether private or not, have no privacy for the internal class
- The external class cannot directly access the internal class, but it can be directly accessed as a normal data type through instantiation. Direct new inner class
The internal class is not defined as public, and the same package can be accessed by default. If you want to access different packages, you must define it as public
The above is the general way to distinguish external class from internal class
What are the internal classes
Inner classes are divided into member inner class, static inner class, local inner class and anonymous inner class.
Member inner class
The following is the definition of a member internal class, and its usage is as follows:
package com.company; /** * Definition of external class and member internal class */ public class Outer < private int outerVariable = 1; private int commonVariable = 2; private static int outerStaticVariable = 3; //Omit getter/setter /** * Member method */ public void outerMethod() < System.out.println("I'm external outerMethod method"); >/** * Static method */ public static void outerStaticMethod() < System.out.println("I'm external outerStaticMethod Static method"); >/** * Inner class */ public class Inner < private int commonVariable = 20; /** * Construction method */ public Inner() < >/** * Member methods to access external class information (properties, methods) */ public void innerShow() < //When it conflicts with the external class, it directly refers to the property name, which is the member property of the internal class System.out.println("Internal commonVariable:" + commonVariable); //Internal classes access external properties System.out.println("outerVariable:" + outerVariable); //When it overlaps with the external class attribute name, the external class name. this. Attribute name can be used System.out.println("External commonVariable:" + Outer.this.commonVariable); System.out.println("outerStaticVariable:" + outerStaticVariable); //Methods to access external classes outerMethod(); outerStaticMethod(); >> /** * External class access internal class information */ public void outerShow() < Inner inner = new Inner(); inner.innerShow(); >public static void main(String[] args)< Outer o = new Outer(); System.out.println("Method call of external o.outerShow(); System.out.println("Initialization call of inner code-block">Method call of external commonVariable:20 outerVariable:1 External commonVariable:2 outerStaticVariable:3 I'm external outerMethod method I'm external outerStaticMethod Static method Initialization call of inner commonVariable:20 outerVariable:1 External commonVariable:2 outerStaticVariable:3 I'm external outerMethod method I'm external outerStaticMethod Static methodThe above contents are explained as follows:
- The Inner class is defined inside the Outer class, which is equivalent to the position of a member variable of the Outer class. The Inner class can use any access control character, such as public, protected, private, etc
- The innerShow method defined in the Inner class can directly access the data in the Outer class without the influence of access control characters, such as the private property commonVariable in the Outer class
- If the external class and the internal class have the same member variables or methods, the internal class accesses its own member variables or methods by default. If you want to access the member variables of the external class, you can use this keyword, such as: Outer.this.name
- The external class can't directly use the members and methods of the internal class. You can first create the objects of the internal class, and then access its member variables and methods through the objects of the internal class;
- After defining an internal class of a member, you must use an external class object to create an internal class object, instead of directly creating a new internal class object, that is: internal class object name = external class object. New internal class ();
- After compiling the above program, you will find that two. Class files are generated: Outer.class,Outer$Inner.class
- There cannot be any static variables and methods in the inner class of a member. Constants can be defined
- Non static inner classes depend on instances of outer classes, while static variables and methods do not depend on objects and are only related to classes. In short, when loading a static domain, there is no outer class at all, so static domains or methods cannot be defined in the non static inner classes, and compilation fails. The scope of non static inner classes is instance level
- Constants are determined by the compiler and put into the constant pool
The above is the use of member inner class, member inner class can be compared to a non static member.
Static inner class
The following is the definition of a static inner class, which is used as follows:
package com.company; /** * Definition of external class and member internal class */ public class Outer < private int outerVariable = 1; /** * External class defined property (duplicate name) */ private int commonVariable = 2; private static int outerStaticVariable = 3; static < System.out.println("Outer The static block of is executed"); >/** * Member method */ public void outerMothod() < System.out.println("I'm external outerMethod method"); >/* * Static method */ public static void outerStaticMethod() < System.out.println("I'm external outerStaticMethod Static method"); >/** * Static inner class */ public static class Inner < /** * Member information */ private int innerVariable = 10; private int commonVariable = 20; static < System.out.println("Outer.Inner The static block of"); >private static int innerStaticVariable = 30; /** * Member method */ public void innerShow() < System.out.println("innerVariable:" + innerVariable); System.out.println("Internal commonVariable:" + commonVariable); System.out.println("outerStaticVariable:"+outerStaticVariable); outerStaticMethod(); >/** * Static method */ public static void innerStaticShow() < //When called, the Outer class is loaded first outerStaticMethod(); System.out.println("outerStaticVariable"+outerStaticVariable); >> /** * How does the interior of an external class deal with an internal class */ public static void callInner() < System.out.println(Inner.innerStaticVariable); Inner.innerStaticShow(); >public static void main(String[] args) < //When accessing the static methods of the static Inner class, the Inner class is loaded, and the outer class is not loaded. It exists independently and does not depend on the outer class. Outer.Inner.innerStaticShow(); //Member methods for accessing static inner classes Outer.Inner oi = new Outer.Inner(); oi.innerShow(); Inner in=new Inner(); in.innerShow(); >>The results are as follows:
Outer The static block of is executed Outer.Inner The static block of I'm external outerStaticMethod Static method outerStaticVariable3 innerVariable:10 Internal commonVariable:20 outerStaticVariable:3 I'm external outerStaticMethod Static methodThere are several differences between static inner class and member inner class
- A static inner class can contain any information, static or non static, because it does not depend on the instance object of the outer class, so it can have static members
- Methods of static inner classes can only access the static decorated information of outer classes.
- Access the static information of static inner class directly Outer.Inner.innerStaticShow(); OK
- Accessing non static information of static inner classes Outer.Inner oi = new Outer.Inner ();, and then oi.innerShow(); OK
- Static inner classes can exist independently and do not depend on other outer classes. When creating objects of static inner classes, you don't need instance objects of outer classes. You can directly create inner class object name = new inner class ();
- If the static members of the external class have the same name as the members of the internal class, you can access the static members of the external class through "class name. Static members"; if the static members of the external class have different names from the members of the internal class, you can directly call the static members of the external class through "member name"
In a word, the loading of static inner class only depends on the class object of outer class, not the instance object. More independent, a static inner class can be compared to a static member.
Local inner class
The following is the definition of a static inner class, which is used as follows:
/** * External class, internal class */ public class Outer < /** * Properties and methods */ private int outerVariable = 1; /** * Properties defined by external classes */ private int commonVariable = 2; /** * Static information */ private static int outerStaticVariable = 3; /** * Member external method */ public void outerMethod() < System.out.println("I'm external outerMethod method"); >/** * Static external method */ public static void outerStaticMethod() < System.out.println("I'm external outerStaticMethod Static method"); >/** * Entry to the program */ public static void main(String[] args) < Outer outer = new Outer(); outer.outerCreatMethod(100); >/** * Member methods, defining local inner classes */ public void outerCreatMethod(int value) < /** * female sex */ boolean sex = false; /** * Local inner class, cannot have access modifier before class */ class Inner < private int innerVariable = 10; private int commonVariable = 20; /** * Local inner class method */ public void innerShow() < System.out.println("innerVariable:" + innerVariable); //local variable System.out.println("Male or not:" + sex); System.out.println("parameter value:" + value); //Information about calling external classes System.out.println("outerVariable:" + outerVariable); System.out.println("Internal commonVariable:" + commonVariable); System.out.println("External commonVariable:" + Outer.this.commonVariable); System.out.println("outerStaticVariable:" + outerStaticVariable); outerMethod(); outerStaticMethod(); >> //Local inner classes can only be used inside methods Inner inner = new Inner(); inner.innerShow(); > >
The results are as follows:
innerVariable:10 Male or not:false parameter value:100 outerVariable:1 Internal commonVariable:20 External commonVariable:2 outerStaticVariable:3 I'm external outerMethod method I'm external outerStaticMethod Static method
Compared with member inner class and static inner class, local inner class has many limitations
- A local inner class is like a local variable in a method. It can't have public, protected, private and static modifiers.
- Local inner classes cannot contain static members.
- Local inner classes can directly access local variables and parameters in methods, and can only access local variables of final type defined in methods, but they cannot be changed.
- The local inner class can access any information of the outer class at will.
When the method is called and run, the local variable is dead. However, inner class objects may still exist and will not die until they are not referenced. In this case, if an internal class wants to access a nonexistent local variable, using the final modifier will not only keep the reference of the object unchanged, In addition, the compiler will continue to maintain the life cycle of the object in the callback method. The local inner class does not directly call the parameters passed in by the method, but the inner class backs up the passed in parameters to its own internal through its own constructor. The internal method calls its own properties instead of the parameters of the external class method, so as to prevent data from being tampered, Local inner classes can be compared to a method parameter.
Anonymous Inner Class
The following is the definition of an anonymous inner class. Its usage is as follows. First, define the interface
/** * The method in the interface is public by default */ public interface IAnimal
Then use the anonymous inner class:
/** * Internal and external classes */ public class Outer < public static IAnimal getInnerInstance(String speak)< return new IAnimal()< @Override public void speak()< System.out.println(speak); >>; //Note that the semicolon on the previous line must have > public static void main(String[] args) < //The call's speak() is an overridden speak method. Outer.getInnerInstance("TTTTTTT").speak(); >>
Anonymous inner classes are common when we simplify code
- An anonymous inner class uses new directly to generate a reference to an object; an anonymous inner class does not have a class name
- There is a flaw in the use of anonymous inner class, that is, it can only be used once. When an anonymous inner class is created, it will immediately create an instance of the class, and the definition of the class will disappear immediately. Therefore, anonymous inner class cannot be reused;
- When using anonymous inner classes, we must inherit a class or implement an interface, but we can't have both. At the same time, we can only inherit a class or implement an interface;
- No constructor can be defined in an anonymous inner class, and no static member variable or static method can exist in an anonymous inner class;
- Anonymous inner class cannot have any static member variables and static methods. Anonymous inner class cannot be abstract. It must implement all abstract methods of inherited class or implemented interface
- Anonymous inner class initialization: use construction code block! Using construction code block can achieve the effect of creating a constructor for anonymous inner class
An anonymous inner class can categorize a method to return a value.
Posted by xudec at Mar 17, 2021 - 11:29 AM
Hot Categories
Hot Tags
- Java × 8678
- Python × 3398
- Algorithm × 2157
- Linux × 2069
- Javascript × 1932
- data structure × 1524
- Spring × 1497
- C++ × 1439
- MySQL × 1163
- Database × 1138
- Front-end × 1057
- Design Pattern × 1024