Detailed explanation of the four inner classes in java

Detailed explanation of the four inner classes of java

Generally speaking, there are 4 types of inner classes: regular inner classes, static inner classes, local inner classes, and anonymous inner classes.

 1. Regular inner classes: Regular inner classes are not modified with static and are defined in the outer class body.
  1. The methods in the regular inner class can directly use the instance variables and instance methods of the outer class.
  2. Objects can be created directly with inner classes in regular inner classes
  3. The code is as follows:

public class MyOuter {
 private int x = 100;

 // create inner class
 class MyInner {
  private String y = "Hello!";

  public void innerMethod() {
   System.out.println("String =" + y in inner class);
   System.out.println("x in the outer class = " + x);// Direct access to the instance variable x in the outer class
   outerMethod();

   System.out.println("x is" + MyOuter.this.x);
  }

 }

 public void outerMethod() {
  x++;

 }

 public void makeInner() {
//create inner class instance in outer class method
  MyInner in = new MyInner();
 }

 /**
  * @param args
  */
 public static void main(String[] args) {

  MyOuter mo = new MyOuter();
  // Create the mo object using the outer class constructor
  MyOuter.MyInner inner = mo.new MyInner();//The regular inner class needs to pass the instance of the outer class to create the object, similar to the instance variable needs to be accessed through the object
  // create inner object
  inner.innerMethod();
  // TODO Auto-generated method stub

 }

}


  4. Running result:

String = Hello in the inner class!
    x =100
    x is101 in the outer class

2. Static inner class: Similar to other members of the class, the inner class can be modified with static. Such a class is called a static inner class. A static inner class is similar to a static inner method. It can only access the static members of the outer class, but cannot directly access the instance variables of the outer class. Unlike instance methods, it can only be accessed through object references.
  3. Since the static inner class does not have any reference to the instance of the outer class, the this keyword cannot be used in the static inner class to access the instance members in the outer class, but the static members in the outer class can be accessed. This figured out with the general class static method
  4. Example code:

public class Java05 {


     public static  int x=100;
     
     
     public static class MyInner{
         private String y="Hello!";
         public void innerMethod(){
             System.out.println("x="+x);
             System.out.println("y="+y);
         }
     }
     
     public static void main(String[] args) {
          Java05.MyInner si=new Java05.MyInner();//Static inner classes can create objects without external instances; similar to class variables that can be accessed by class name
          si.innerMethod();
          // TODO Auto-generated method stub

         }
}

3. Local inner class: A class defined inside a method body or statement block (including methods, constructors, local blocks or static initialization blocks) becomes a local inner class.
  A local inner class cannot have any access modifiers because it is only valid for local blocks.
  1. The local inner class is only valid in the method body. Just like the defined local variables, the object of the local inner class cannot be created outside the defined method
  . 2. When defining a class inside a method, you should pay attention to the following issues:
   1. Method definition A local inner class is the same as a method to define a local variable. It cannot be modified with access modifiers such as private, protected, and public, nor can it be modified with static. However, it can be modified with final and abstract
   . 2. The inner class in the method can access the members of the outer class. For the parameters and local variables of the method, they must be final modified before they can be accessed.
   3. The inner class defined in the static method can access the static members defined by the outer class

4. Anonymous inner class: The ultimate purpose of defining a class is to create an instance of a class, but if an instance of a class is only used once, you can put the class definition and class creation together to complete, or in the definition When a class is created, a class
  defined in this way without a name becomes an anonymous inner class.
   The general format for declaring and constructing anonymous inner classes is as follows:
   new ClassOrInterfaceName(){

    /*Class body*/ }

   1. An anonymous inner class can inherit a class or implement an interface, where ClassOrInterfaceName is the name of the class inherited by the anonymous inner class or the name of the interface implemented. But anonymous inner classes cannot implement an interface and inherit a class at the same time, nor can they implement multiple interfaces. If an interface is implemented, the class is a direct subclass of the Object class, and the anonymous class inherits a class or implements an interface, the extends and implements keywords are not required.

2. Since the anonymous inner class has no name, the constructor cannot be defined in the class body, and the keyword cannot be used to create an instance of the class because the class name is not known. In fact, the definition, construction, and first use of anonymous inner classes all take place in the same place. Also, the above expression is an expression that returns a reference to an object, so it can be used directly or copied to an object variable. example:

   TypeName obj=new Name(){

   /*This is the class body*/
      }
  Likewise, it is also possible to pass a constructed object as an argument to the call. example:

someMethod(new Name(){
   /*Class body here*/ });

   3. Program code:
  public class NiMing {
 private int size=5;
 public Object makeInner(int localVar){
  final int finalLocalVar=localVar;
  return new Object(){
   // use anonymous inner class
   public String toString(){
    return "OuterSize="+size+"\nfinalLocalVar="+finalLocalVar;


   }


  };


 }

 /**
  * @param args
  */
public static void main(String args[])
 {
 Object obj=new NiMing().makeInner(47);
 System.out.println(obj.toString());

}

}


 4. Program running result:
   OuterSize=5
   finalLocalVar=47

Measure
Measure

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325633422&siteId=291194637