This is the 25th day of my participation in the August Genwen Challenge.More challenges in August

The inner class

Basic overview

  • When the definition of a class is placed in the entity of another class, that class is called an inner class, and the class in which that class resides is called an outer class
  • Things that can occur in a class body: member variables, member methods, constructors, constructor blocks, static statement blocks, static variables, methods, inner classes
  • Nested classes
    • Inner classes (member inner classes, local inner classes, anonymous inner classes)
    • Statically nested classes

Syntax format

Class external class class name {class inner class name {inner class body; }}Copy the code

Member inner class

  • Member inner classes define inner classes in another class or interface
  • Matters needing attention
    • An external class object must be created before a member inner class object can be created
    • Cannot contain static variables, static code blocks, or static methods (except static constants)
    • An external class can call an inner class private member from an object of its inner class
    • A member inner class is a separate class compiled into a separate.class file
  • role
    • Member inner classes can access both external class information and parent class information, completing a multi-inheritance solution
  • case
    • Outer.java
    // The use of member inner classes
    public class Outer { / / outside class
    	
    	private String str = "The str1 Outer class";
    	private String str2 = "The str2 Outer class";
    
    	/* * First: the name of the inner class cannot be the same as the name of the outer class * second: the final, access modifier can be used to modify */
    	public class Inner{ / / inner classes
    
    		private String str = "The str1 Inner class";
    		private String str2 = "The str2 Inner class";
    		
    		public void show(a) {
    			// Members of inner classes take precedence over members of outer classes
    			System.out.println(str);
    			System.out.println(str2);
    			
    			// Use "external class.this. member" to access members of an external class
    			System.out.println(Outer.this.str2);
    			System.out.println(Outer.this.str); }}}Copy the code
    • TestOuter.java
    import Inner.demo1.Outer.Inner;
    
    public class TestOuter {
    
    	public static void main(String[] args) {
    		// If you want to create an inner class, you need to create an outer class object first
    		
    		// Create an external class object
    		Outer o = new Outer();
    	
    		// Create inner class
    		Inner inner = o.new Inner(a); inner.show(); }}Copy the code
    Str1 Inner str2 Outer str2 Outer str1 Inner str2 Outer str1 Inner str2 OuterCopy the code

Local inner class

  • A local inner class is an inner class defined in a method H or code block

  • Matters needing attention:

    • Cannot contain static variables, static code blocks, or static methods
    • Can only be used in the method or code block that defines the class and must be defined before use
    • J local variables must be valid constants when accessing the local variables of all its methods
    • Is a separate class compiled into a separate.class file
    • Only abstract and final can be used
    • When defining a static block or method, only the static members of the external class can be accessed
  • case

    • Outer.java
    // Local inner class
    public class Outer { / / outside class
    
    	private String str1 = "Str1 of class Outer";
    	private String str2 = "Str2 of class Outer";
    	
    	// Define a method of an external class
    	public void print(a) {
    		System.out.println("Print method in class Outer");
    	}
    	
    	static {
    		class Inner{}  // Local inner class
    	}
    	
    	public void method(a) {
    		
    		// Must be defined before use
    		class Inner{
    			private String str1 = "Str1 in Inner class";
    			
    			Inner class members take precedence over outer class members
    			public void visitOuter(a) {
    				System.out.println(str1);
    				System.out.println(str2);
    				print();  // Skip directly to the outer class method}}// A local inner class can only be used in the method or code block that defines it
    		// Only abstract/final can be used. It cannot have the same name as an external class
    		Inner in = newInner(); in.visitOuter(); }}Copy the code
    • TestOuter.java
    public class TestOuter {
    
    	public static void main(String[] args) {
    		
    		Outer o = newOuter(); o.method(); }}Copy the code
    Inner str1 Outer str2 Outer print methodCopy the code

Anonymous inner class

  • An anonymous inner class is an inner class created without a name when instantiated directly using an interface or parent class

  • Syntax format

    • Interface/superclass type reference name = new interface/superclass type (){override method;

    }

  • Matters needing attention

    • You must and can only inherit a parent class or implement an interface
    • No class keyword, no class name
    • Are special local inner classes
    • Can only be used once
    • A constructor cannot be defined
    • Anonymous classes cannot be abstract classes
  • Advantages and Functions

    • Anonymous inner classes can make code more compact and concise
    • Encapsulation is better
    • Anonymous inner classes can be created flexibly
    • Anonymous inner classes make a multi-inheritance solution complete
  • case

    • InterDemo.java
    public interface InterDemo {
    
    	public abstract void print(a);
    }
    Copy the code
    • Outer.java
    public class Outer { / / outside class
    
    	private String str1 = "The Outer class str1";
    	private String str2 = "The Outer class str2";
    	
    	// Anonymous inner class
    	InterDemo id = new InterDemo() {
    		
    		private String str1 = "Anonymous inner class str1";
    		
    		@Override
    		public void print(a) {
    		
    			System.out.println(str1);
    			System.out.println(str2);
    			
    			// If you want to call an external class variable, the external class.this. variable
    			System.out.println(Outer.this.str1); }};public void show(a) { id.print(); }}Copy the code
    • TestOuter.java
    public class TestOuter {
    
    	public static void main(String[] args) {
    		
    		Outer o = newOuter(); o.show(); }}Copy the code
    Anonymous inner class str1, str2, str1 OuterCopy the code

Statically nested classes

  • Static nested classes define nested classes in another class, on an interface, and decorated with the static keyword

  • Matters needing attention

    • There is no need to generate an external class object to generate a statically nested class object
    • Only static members of an external class can be accessed directly
    • An external class can call a member of an inner class through an object of a statically nested class
    • Static member variables or static member methods can be defined
  • case

    • Outer.java
    public class Outer { / / outside class
    
    	// Create two member variables, one static and one non-static
    	private String str1 = "Str1 of external class";
    	private static String str2 = "Str2 for external classes";
    	
    	// Member method, static
     	public static void show1(a) {
    		System.out.println("Show methods for external classes");
    	}
    
    	// statically nested classes
    	// Can only be defined in a member of a class, not in a method or code block
    	public static class Inner{
    		
    		private String str1 = "Str1 for nested classes";
    		// private static String str2 = "private static String str2";
    		
    		public void show(a) {
    			System.out.println(str2);
    			System.out.println(str1);
    			
    			// Non-static members of an external class cannot be accessed directly
    			// But you can access the static members of the external class directlyshow1(); }}}Copy the code
    • TestOuter.java
    import Inner.demo4.Outer.Inner;
    
    public class TestOuter {
    	public static void main(String[] args) {
    		
    		// How do I create inner classes or statically nested class objects
    		// First create an object for the external class
    		Inner Inner = newOuter.Inner(); Inner.show(); }}Copy the code
    Show method of str1 outer class of str2 nested classCopy the code

The difference between statically nested and non-statically nested classes

The name of the Inner classes (non-statically nested classes) Statically nested classes
Define position Member locations, methods, code blocks Only member locations of external classes
composition Instance members, static constants, constructors Instance member, static member, static code block, constructor
Object creation There must be an object of an external class first Does not depend on external class instances and can be instantiated directly
Accessing external classes All members of an external class can be accessed directly Only static members of an external class can be accessed directly

Finally, welcome to pay attention to my personal wechat public account “Little Ape Ruochen”, get more IT technology, dry goods knowledge, hot news