banner
jzman

jzman

Coding、思考、自觉。
github

Inner Classes in Java Series

Today, let's review the inner classes in Java. Generally, there are several types of inner classes: static inner class, member inner class, anonymous inner class, and method inner class. The following text will mainly introduce static inner class and member inner class, with the main content as follows:

  1. Overview
  2. Static Inner Class
  3. Member Inner Class
  4. Anonymous Inner Class and Method Inner Class
  5. Summary

Overview#

As the name suggests, an inner class is a class defined within another class. It is an independent class, and after compilation, it will generate a separate .class file, with the outer class name and $ symbol in front of its name. Inner classes can also be modified by private, default, protected, and public. The following is the syntax for inner classes, please refer to the specific code:

/**
 * Inner class
 * @author jzman
 */
public class OutterClass {
	// Static inner class
	private static class StaticInnerClass{
		
	}
	// Member inner class
	private class FieldInnerClass{
		
	}
	
	// Anonymous inner class
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			
		}
	};
	
	private void method() {
		// Method inner class (local inner class)
		class InnerClass{
			
		}
	}	
}

Static Inner Class#

  • Static inner class can have static members and non-static members.
  • Static inner class can directly access the static members and static methods of the outer class, but cannot access the non-static members and non-static methods of the outer class.
  • In unrelated classes, you can directly create objects of static inner class.
  • Static inner class has little connection with its outer class, and its creation does not depend on the outer class.
/**
 * Static inner class demo
 */
public class StaticInnerClassDemo {
	public static void main(String[] args) {
		// Other classes can directly create instances of a non-private inner class
		OutterClass1.InnerClass innerClassB = new OutterClass1.InnerClass();
		InnerClass innerClassA = new InnerClass();
		innerClassA.testStaticInnerClass();
		//...
	}
}

// Outer class
class OutterClass1{
	// Non-static member variable
	int ageA = 18;
	// Non-static method
	public void methodA() {
		
	}
	
	// Static member variable
	static int ageB = 18;
	// Static method
	public static void methodB() {
		
	}
	
	private void methodC() {
		// Outer class calls the method of static inner class
		InnerClass innerClass = new InnerClass();
		innerClass.testStaticInnerClass();
	}
	
	// Static inner class
	static class InnerClass{
		
		public void testStaticInnerClass(){
			// Static inner class cannot access the non-static member variables and methods of the outer class
//			System.out.println(ageA);
//			methodA();
			// Static inner class can access the static members and methods of the outer class
			System.out.println(ageB);
			methodB();
		}
	}
}

Member Inner Class#

  • Member inner class can access all the members of the outer class.
  • The creation of a member inner class object must depend on an outer class object. The inner class can only exist after the outer class exists.
  • Member inner class is a member variable of the outer class.
  • Member inner class cannot have static members, but allows constants to exist.
/**
 * Member inner class demo
 */
public class FieldInnerClassDemo {
	public static void main(String[] args) {
		// Member inner class depends on its outer class, cannot directly create an instance of the inner class without the outer class
//		InnerClass innerClass = new InnerClass();
		// Correct way to create an instance of the inner class in another class
		OutterClass2 outterClass2 = new OutterClass2();
		OutterClass2.InnerClass innerClass = outterClass2.new InnerClass();
		innerClass.testFieldInnerClassMethod();
	}
}

// Outer class
class OutterClass2{
	// Non-static member variable
	int ageA = 18;
	// Non-static method
	public void methodA() {
		
	}
	
	// Static member variable
	static int ageB = 18;
	// Static method
	public static void methodB() {
		
	}
	
	private void methodC() {
		// Outer class calls the method of member inner class
		InnerClass innerClass = new InnerClass();
		innerClass.testFieldInnerClassMethod();
	}
	
	// Member inner class
	class InnerClass{
		
		// Member inner class cannot have static members
//		static int a = 10;
		// Member inner class allows constants to exist
		static final int b = 10;
		
		public void testFieldInnerClassMethod(){
			// Member inner class can directly access all the member variables and methods (static and non-static) of its outer class
			System.out.println(ageA);
			methodA();
			System.out.println(ageB);
			methodB();
		}
	}
}

Anonymous Inner Class and Method Inner Class#

Anonymous inner class does not have a specific name. Its implementation is actually implemented in the class body, and it finally returns an anonymous object of the relevant class. Since the final return is an anonymous instance, a semicolon must be added after the anonymous inner class. Method inner class is a local inner class, generally defined in the local position of a class, and it can access all the variables of the current code block and the outer class.

Summary#

The mutual calling of member variables between classes is actually accessing the member variables of objects. For example, the member inner class can access the member variables or methods of the outer class because the inner class holds a reference to the outer class. Generally, the outer class can access the member variables or methods of the inner class by holding a reference to the inner class. Therefore, when the outer class accesses the member variables or methods of the inner class, it must first create an object of the inner class, and then use the object to access the corresponding member variables or methods.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.