Summary of four inner classes in Java

1. Static inner class

How to write a static inner class?

   

class OuterClass{
	private int a = 10;//Instance data member
	private int b = 20;
	private static int c = 30;//Static member
	private static final int d = 40;//The static data member modified by final, the value cannot be changed
	
	public OuterClass(){//Constructor of outer class
		System.out.println("OuterClass()  init~~~~");
	}
	
	public void show(){
		System.out.println("OuterClass()  show~~~~");
	}
	//By static modification, static inner class
	static class InnerClass{
		private int data1 = 10;
		private int data2 = 20;
		private int c = 100;
		private static int data3 = 30;
		
		public InnerClass(){//Constructor of inner class
			System.out.println("InnerClass  init~~~~");
		}
		public void show2(){
			System.out.println("InnerClass  show2()~~~~");
			System.out.println("innerclass c:"+c);
			System.out.println("outerclas c:"+OuterClass.c);//The c of the outer class is static,
								//Access by class name.Data member
			System.out.println("d:"+d);
			
		}
		
		
	}
	
}
public class TestDemo2 {
	
	// static inner class
	public static void main(String[] args) {
		//InnerClass in2 = new InnerClass();//Don't write like this, it will report an error!
		OuterClass.InnerClass in = new OuterClass.InnerClass();
		in.show2();
	}
}

The running result is shown in the figure

When accessing the static data members of the outer class in a static inner class, there is no problem, but if you access the instance data members of the outer class, an error will be reported. As shown


So how does a static inner class access the instance member variables of the outer class?
 1. Because the instance member variables depend on the object of the outer class
 2. Can an outer class object be passed in (passed in by the constructor)
 3. Instantiate one in the main function The outer class object OuterClass outer = new OuterClass();
 4. In the constructor of the inner class, pass an instance member variable of the outer class OuterClass o2;
 5. Receive the outer class object o2 6. o2
 has already got the outer class object
 7.System .out.println("a:"+o2.a);

static class InnerClass{
		private int data1 = 10;
		private int data2 = 20;
		private int c = 100;
		private static int data3 = 30;
		OuterClass o2;
		
		public InnerClass(OuterClass o){//Constructor of inner class
			o2 = o;//Pass in the external class object through the constructor
			System.out.println("InnerClass  init~~~~");
		}
		public void show2(){
			System.out.println("InnerClass  show2()~~~~");
			//System.out.println("a:"+a);//The members of the external class cannot be accessed, so how to access? ? ? ?
			System.out.println("outerclass a:"+o2.a);//Access the non-static data members of the outer class through the outer class object
			System.out.println("innerclass c:"+c);
			System.out.println("outerclass c:"+OuterClass.c);//The c of the outer class is static,
				                            //Access by class name.Data member
			System.out.println("d:"+d);
			
		}
public static void main(String[] args) {
		OuterClass outer = new OuterClass();
		OuterClass.InnerClass in = new OuterClass.InnerClass(outer);
		in.show2();
	}

In this way, you can access the instance data members of the outer class.


2. Instance inner class

class OuterClass2{
	private int data1 = 1000;
	private int a = 20;
	private int b = 20;
	private static int c = 30;//Static member
	private static int d = 40;
	
	class InnerClass{//Instance inner class, similar to instance member methods, depends on the outer class object
		private int data1 = 10;
		private int data2 = 20;
		//private static int data3 = 30;//There cannot be static member methods in the inner class of the instance, an error will be reported
		private static final int data4 = 30;//The immediate number determined at compile time
		//private static final int data5 = data1 + 30;
		//static InnerClass in = new  InnerClass();
		public void show2(){
			System.out.println("InnerClass  show2()~~~~");
			System.out.println("data1:"+data1);//It is an inner class
			System.out.println("data1:"+this.data1);//It is also an inner class
			System.out.println("outer data1:"+OuterClass2.this.data1);//This method can access the data members of the outer class
			System.out.println("b:"+b);//Can be accessed, c and d can also
		}
		
		public InnerClass(){
			System.out.println("InnerClass  init~~~~");
		}
	}
	public OuterClass2(){
		System.out.println("OuterClass()  init~~~~");
	}
	
	public void show(){
		System.out.println("OuterClass()  show~~~~");
	}
}

Create instance inner class object

public static void main(String[] args) {
		OuterClass2 outer = new OuterClass2();
		//Instantiate an inner class that depends on the outer class object
		OuterClass2.InnerClass in = outer.new InnerClass();
		in.show2();
	}

1. Why an immediate number that can be determined during compilation must be defined in the instance inner class
  ---Explain ① the initialization process of a class, first static and then the instance method 
  ---Explain ② the design meaning of the instance inner class
  ------ -Each instance inner class must have an outer class object, relying on the outer class object
   assumption: static InnerClass in = new InnerClass(); Establishing
   OuterClass.InnerClass.in can directly get an instance of the instance inner class, which
     goes against the design meaning (depending on outer class object), there is a conflict.
  2. Does the instance inner class have additional memory consumption?

  ---Yes, and the this reference variable of the outer class


3. Local inner class

class inside method

//local inner class, the class inside the method
	public void test(){
		final int data6 = 10;//local variable
		class InnerClass{
			private int a = 10;
			//private static int b = 20;//Error
			private static final int c = 30;//Immediate number determined during compilation
			public InnerClass(){
				System.out.println("test() InnerClass  Init~~~~");
			}
			
			public void show(){
				System.out.println("a: "+a);
				System.out.println("c: "+c);
				System.out.println("data6: "+data6);
				System.out.println("test() show~~~~");
			}
		}
		InnerClass in = new InnerClass();
		in.show();
	}


4. Anonymous inner class

equivalent to a subclass of the current class


public class TestDemo3 {
	
	public static void test(Student s){
		s.show();
	}
	
	//Anonymous inner class, equivalent to a subclass of the current class
	public static void main(String[] args) {
		new Student(){
			public void show(){
			System.out.println("niming  show()~~~~");
			}
		}.show();
		
	/*	test(new Student(){
			public void show(){
			System.out.println("niming  show()~~~~");
			}
		});*/
		
		
	}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326843145&siteId=291194637