Java internal class usage summary, java class usage Summary
1. What is an internal class?
The class defined in the class is called the internal class.
Public class Out {class In {// at this time, In is an internal class }}
2. Why use internal classes?
1), enhance encapsulation, hide internal classes in external classes, and do not allow other classes to access internal classes
2), internal classes can improve code readability and maintainability
3. Classification of internal classes
The classification of internal classes can be compared with the classification of member variables.
The member variables can be subdivided into class member variables, instance member variables, and local variables based on different modifiers or definitions.
As a member of an external class, the internal class can be modified using public/default/protected/private or static.
Likewise, internal classes are divided into four categories based on different modifiers or definitions:
1) instance internal class: internal class does not use static Modification
2) static internal class: the internal class is modified using static
3) Local internal class: internal class defined in the Method
4) Anonymous internal class: it can only be used once. It is a special case of internal class.
3.1 instance internal class:
1) Definition: the internal class of the instance, that is, the internal class without static modification. This indicates that the internal class of the instance belongs to the object of the external class and does not belong to the external class itself (analogy field ).
2) create an instance internal class
// External class Outter {// internal class of the instance: the class Inner {} public class InnerDemo1 {public static void main (String [] args) is not modified using static) {// create an internal class of the instance without static modification. It is an object of an external class. Therefore, before creating an internal class of the instance, an external Class Object Outter out = new Outter () must exist (); // create an internal Class Object Outter through an external class object. inner in = out. new Inner ();}}
3) features:
A. The process of creating an instance's internal class shows that when an internal class object exists, there must be an external class object.
B. Instances of the internal class of the instance automatically hold the reference of the instance of the external class. The internal class of the instance can unconditionally access all fields and methods of the external class.
Note: When a Member's internal class has a member variable or method with the same name as the external class, this hidden phenomenon occurs.
C. The external class cannot directly access the members of the Internal class. You must first create an object of the internal class of the member and then access it by referencing the object.
// External class Outter {private String name = "out"; private Integer age = 17; // instance internal class: class Inner {private Integer age = 18 is not modified using static; // hide the external class's age Inner () {// features: 1. the internal class of the instance can directly access external class members // 2. when the instance's internal class and external class have fields or methods of the same name, the System will hide the phenomenon. out. println (name + this. age); // output out18 // if you need to use the age of the external class, Syntax: External class. this. age System. out. println (Outter. this. age); // output 17 }}}
Summary: Simply put, it is to look at the scope of the variable. The scope of the external class member variable is the entire external class, while the internal class is in the external class (can be seen as the field of the external class ), internal classes can naturally access external classes. the external class needs to access the members of the Internal class. It can be understood that the members of the Internal class belong to the internal class, which is valid in the internal class and does not exist in the internal class, and the member variables do not exist, therefore, the external class cannot directly access the members of the Internal class. You must first create an object of the internal class of the member and then access it by referencing the object.
3.2 static internal class
1) Definition: internal class modified using static. Therefore, this internal class belongs to the external class itself, not the object of the external class.
2) create a static internal class
// External class Outter {// static internal class: use static to modify static class Inner {} public class InnerDemo2 {public static void main (String [] args) {// because the static internal class belongs to the external class itself, you can directly access (analogy field) Outter through the external class name. inner in = new Outter. inner ();}}
3) features:
A. When creating an internal class instance, you do not have to create an external class instance.
B. The static internal class can directly access the static members of the external class. If you access the instance members of the external class, you must access the static members through the instance of the external class.
Simple understanding: static members belong to the class, and non-static members belong to the object. If you want to access the instance members of the external class (non-static members), of course, you must first store the external class object. the creation of static internal classes does not require external class objects. Therefore, if you access instance members of external classes, you must access them through external class instances.
C. You can define static members and instance members in a static internal class.
D. The test class can directly access the static members of the static internal class through the complete class name.
// External class Outter {static String name = "outter"; public Integer age = 17; // static internal class: use static to modify static class Inner {Inner () {// The static internal class can directly access the static member System of the external class. out. println (name); // output outter // The instance Member accessing the external class, which must be accessed through the instance of the external class. system. out. println (new Outter (). age); // output 17 }}}
3.3 Local internal classes (rarely used)
1) Definition: the internal class defined in the method. Its visible range is the current method, which is at the same level as the local variable. Therefore, the local internal class can only be used in the method.
Note that local internal classes and local variables in the method are the same. public, protected, private, and static modifiers are not allowed.
Public class InnerDemo3 {public static void main (String [] args) {// partial internal class Inner {}}}
2) features:
A. The local internal class is the same as the instance internal class and cannot contain static members. (the local internal class belongs to the method, while the static member belongs to the class)
B. Local internal classes and instance internal classes can access all members of the external class.
C. Local variables accessed by local internal classes must be modified using final. in Java 8, final (syntactic sugar) is automatically and implicitly added ).
Cause: After the method is called and run, the stack frame of the current method is destroyed, and all the space of local variables in the method is destroyed. however, internal class objects may still exist in the heap memory and will not die until they are not referenced. in this case, an internal class needs to access a non-existent local variable. to avoid this problem, we use final to modify the local variable and change it to a constant in the memory space. Even after the method is destroyed, the local variable is in the memory and the object can be held.
Public class InnerDemo3 {public static void main (String [] args) {int age = 17; final int num = 15; // partial internal class Inner {public void test () {// error: Cannot refer to a non-final variable age inside an inner class defined in a different method System. out. println (age); System. out. println (num); // correct }}}}
3.4 anonymous internal classes (most frequently used)
1): Definition: An anonymous internal class is a local internal class without a name. It is suitable for only one class.
2) create an anonymous internal class:
The anonymous internal class does not have a constructor, but the parent class constructor is called. in general, anonymous internal classes are used to inherit other classes or implement interfaces. No additional methods are required, but only the implementation or rewriting of the inherited methods.
Note: An anonymous internal class must inherit one parent class or implement one interface, but only one parent class or one interface can be implemented at most.
// Define an interface Person {public void eat ();} public class AnonymousDemo {public static void main (String [] args) {// use the anonymous internal class Person p = new Person () {public void eat () {System. out. println ("eat something") ;}}; p. eat ();}}
4. Summary
5. Interview Questions
public class Outer { public void someOuterMethod() { // Line 3 } public class Inner { } public static void main(String[] argv) { Outer o = new Outer(); // Line 8 }}/* * Which instantiates an instance of Inner?A. new Inner(); // At line 3B. new Inner(); // At line 8C. new o.Inner(); // At line 8D. new Outer.Inner(); // At line 8 */
Answer A. new Inner (); equivalent to this. new Inner (); an Outer Class Object already exists.
Line 8 should be written as follows: o. new Inner ();