Summary of the four inner classes in Java

In the Java language, a class can be defined inside another class, the class inside the class is called the inner class, and the outside class is called the outer class. In this case, the inner class can be seen as a member of the outer class (similar to class properties and methods). There is also a class called a top-level class, which refers to a class whose class definition code is not nested within other class definitions.
There are many types of inner classes, including the following four: static inner class, member inner class, local inner class, and anonymous inner class. They are defined as follows:

class outerClass
{
    
        static class innerClass{
    
    }  //静态内部类}

class outerClass
{
    
        class innerClass{
    
    }  //成员内部类(普通内部类)}

class outerClass
{
    
        public void menberFunction()    {
    
            
	class innerClass{
    
    }   //局部内部类   
	}
}

public class MyFrame extends Frame
{
    
      //外部类  public MyFrame()  
	{
    
            addWindowListener(new WindowAdapter()        
		{
    
      //匿名内部类            
			public void windowClosing(WindowEvent e){
    
    
			    dispose();
			    System.exit(0);
			    }        
			  });
	  }
}

A static inner class refers to an inner class declared as static, which can be instantiated without relying on an instance of the outer class, while the usual inner class needs to be instantiated after the outer class is instantiated. A static inner class cannot have the same name as the outer class, cannot access the ordinary member variables of the outer class, but can only access static members and static methods (including private types) in the outer class.

A static inner class, if the static keyword is removed, becomes a member inner class.

A member inner class is a non-static inner class, which can freely refer to the properties and methods of the outer class, whether these properties and methods are static or non-static. But it is bound to an instance and cannot define static properties and methods. This inner class can only be instantiated after the outer class is instantiated. It is important to note that non-static inner classes cannot have static members.

Local inner class refers to a class defined in a code block, its scope is the code block where it is located, and it is the least used type of inner class. Local inner classes, like local variables, cannot be modified by public, protected, private and static. For a static inner class, remove the "static" keyword in its declaration and move its definition into the static method or static initialization code segment of its outer class to become a local static inner class. For a member class, moving its definition into the instance method or instance initialization code of its outer class becomes a local inner class.

Local static inner classes have the same basic characteristics as static inner classes. Local inner classes have the same basic characteristics as inner classes.

An anonymous inner class is an inner class without a class name, does not use the keywords class, extends, implements, and has no constructor. It must inherit (extends) other classes or implement other interfaces.

The general benefit of anonymous inner classes is that the code is more concise and compact, but the problem is that the readability is reduced. It is generally used in GUI (Graphical User Interface, Graphical User Interface) programming to implement event processing and so on. There are a few principles to keep in mind when using anonymous inner classes:

1) An anonymous inner class cannot have a constructor.
2) Anonymous inner classes cannot define static members, methods and classes.
3) An anonymous inner class cannot be public, protected, private, or static.
4) Only one instance of an anonymous inner class can be created.
5) An anonymous inner class must be behind new, and the anonymous class must inherit a parent class or implement an interface.
6) Because the anonymous inner class is a local inner class, all the restrictions of the local inner class take effect on it.

Guess you like

Origin blog.csdn.net/david2000999/article/details/121451518