Summary of the use of Java internal classes

☆: why use internal classes:
The most attractive reason for using inner classes is that each inner class can inherit an implementation independently, so whether the outer class has inherited an implementation or not has no impact on the inner class.
Inner class: as the name suggests, it is to put the class in a certain range
Classes can be placed in packages
Classes can be placed in folders
Class can be placed in class / / inner class
Classes can be placed in methods / / inner classes

1, Member inner class
Class, inner class, set class in class, class in class
To use an internal class, you need to instantiate the object of the internal class in the external class, and then call the method in the internal class through object management to operate the internal class data through the method.
The inner class can directly use the properties and methods of the outer class, just as the monkey king gets into the belly of Princess Iron Fan.

 

 1 public class OuterClass {
 2     //Properties in external classes
 3     private int outer_1=100;        //Private property    
 4     public  int outer_2=10;            //Public attribute
 5     
 6     //External class method outer_method1
 7     public void outer_method1(){
 8         System.out.println("outer_method1");
 9         InnerClass ic=new InnerClass();
10         //Call methods in internal classes
11         ic.inner_method1();            
12         //Call the private and public properties in the inner class
13         System.out.println(ic.inner_1+" "+ic.inner_2);    
14     }
15     
16     //Named inner class InnerClass
17     class InnerClass{
18         //Properties in Inner Classes
19         private int inner_1=123;    //Private property
20         public int inner_2=321;        //Public attribute
21         //Inner class method_ method1
22         public void inner_method1(){
23             //Call a method in an external class,
24             outer_method1();
25             //Call private and public properties in external classes
26             System.out.println(outer_1+" "+outer_2);
27         }    
28     }    
29 }    

2, Local inner class
Method
To use the inner class in a method, you must instantiate the object of the inner class under the definition of the inner class in the method, and then call the method of the inner class through the management of the inner class object,
The scope of the internal class is limited to the current method. The scope disappears from the beginning of the definition of the internal class to the end of the method.
The local variables in the current method in the external class can be used directly in the internal class, and the internal class in the method cannot be used outside the method.

 1 /**
 2  *Method 
 3  */
 4 public class OuterClass {
 5     //Properties in external classes
 6     private int outer_1=100;        //Private property
 7     public  int outer_2=10;            //Public attribute
 8     //Methods in external classes
 9     public void outer_method1(){
10         System.out.println("outer_method1()");
11         System.out.println("outer_1"+"outer_2");
12         
13         //Inner class in method
14         class InnerClass{
15             //Properties in Inner Classes
16             private int inner_1=123;    //Private property
17             public int inner_2=321;        //Public attribute
18             //Public methods in Inner Classes
19             public void inner_method1(){
20                 System.out.println("public method1");
21                 System.out.println(outer_1);
22             }    
23             //Private methods in Inner Classes
24             private void inner_method2(){
25                 System.out.println("private method2");
26                 System.out.println(outer_1+" "+outer_2);
27             }    }
28         //Instantiate inner class
29         InnerClass ic=new InnerClass();
30         ic.inner_method1();
31         ic.inner_method2();
32         System.out.println(ic.inner_1+" "+ic.inner_2);    
33         //The scope ends here
34     }    
35 }

 

3, Anonymous inner class
1. Anonymous inner classes do not have access modifiers.
2. Sometimes I prefer to use anonymous inner class in order to avoid naming the inner class, because it has no name.
3. Anonymous inner classes have no constructor. Because it doesn't even have a name to construct.
4. Currently, the methods implemented in the anonymous inner class can only be implemented once. If you want to implement other methods, you must re implement new with the anonymous inner class.

 1 public class Demo {
 2     public void TetMethod1(){    
 3         //The anonymous inner class new is the sub implementation of CommonPerson, but there is no class name. The object of the sub implementation is assigned to the elder reference
 4         CommonPerson cp=new CommonPerson(){
 5             @Override
 6             public void study() {
 7             }
 8             @Override
 9             public void eat() {
10             }
11         };
12     }
13     public static void main(String[] args) {
14         //Named inner class
15         class Student implements IPerson{
16             public void study(){}
17             public void eat(){}
18         }
19         Student stu1=new Student();
20         IPerson ip=stu1;
21         
22         //Anonymous Inner Class 
23         IPerson ip1=new IPerson(){
24             public void study(){}
25             public void eat() {}
26         };
27         IPerson ip2=new IPerson(){
28             public void study(){}
29             public void eat() {}
30         };    
31     }
32 }
33 
34 
35 public interface IPerson {      //Defined interface IPerson
36 
37     public void study();
38     public void eat();
39 }

 

4, Nested inner class
Nested inner classes are divided into static inner classes and non static inner classes
1. Static internal class
1. Its creation does not need to depend on external classes.
2. It cannot use non static member variables and methods of any peripheral class.
2. Non static internal class
1. Its creation needs to depend on external classes.
2. After compilation, a reference will be implicitly saved. The reference refers to the peripheral class that created it. You can call the members of the external class at will, whether static or non-static, which is quite overbearing.

 1 public class OuterClass {
 2     private String sex;
 3     public static String name = "ma";
 4     /**
 5      *Static inner class
 6      */
 7     static class InnerClass1{
 8         /* Static members can exist in static inner classes */
 9         public static String name1 = "1_static";
10         public void display(){
11                   //Static inner classes can only access static member variables and methods of outer classes
12             System.out.println("OuterClass name :" + name);
13         }    }
14     /**
15      * Non static inner class
16      */
17     class InnerClass2{
18         /* Static members cannot exist in a non static inner class */
19         public String name2 = "2_inner";
20         /* Non static inner classes can call any member of the outer class, whether static or non static, which is a bit overbearing*/
21         public void display(){
22             System.out.println("OuterClass name: " + name);
23         }
24     }
25     public void display(){
26         // External classes access static internal classes
27         System.out.println(InnerClass1.name1);
28         new InnerClass1().display();  
29         /* Non static internal creation needs to rely on external classes */
30         OuterClass.InnerClass2 inner2=new OuterClass().new InnerClass2();
31         System.out.println(inner2.name2);
32         inner2.display();
33     }  
34 }

Keywords: Java

Added by umasankar on Sat, 05 Feb 2022 12:13:36 +0200