In Java, you can define member variables and methods within a class, and you can define classes within the class. This class is defined within the class as an inner class. The inner class is called the outer class.
There is a separate. class file that can also produce objects.
. java files are not necessarily all file names and class names consistent, only the class name of the public (common class) type requires the. java file name to be consistent with the class name. At the same time, there is only one class in the. java file, but it can have multiple classes (because it allows for internal classes and other classes), so A. java file can have multiple. class files.
Internal classes can generally be divided into 2 types (by position), that is, members ' internal classes and local inner classes. A special case of a member's inner class is the static inner class, and a special exception to the local inner class is the anonymous inner class. The following details distinguish these internal classes.
A member's inner class is defined within a class, as a class member. The format of the. class file that he generated at compile time is: External class name $ member internal class name. class. Before you create an object for a member's inner class, you need to instantiate an object of an external class. A member inner class can call all members of an external class.
A static inner class is defined within a class, use the keyword static as a static member of a class. The format of the. class file that he generated at compile time is: External class name $ static internal name. class, you can directly create objects of static inner classes without creating the I of an external class object. Static inner classes can reference static member variables or static methods of an external class, but cannot refer to an external ordinary member.
A local inner class is a class that is defined within a method. It can only be used within a method, and once the method has been executed, the local inner class is released from memory. The. class file that he generated at compile time is: The external class name is the internal class name. class, which creates the object of the local inner class directly inside the method.
Anonymous inner classes are also special local inner classes, which have no class names, and the definition of anonymous inner classes is merged with the creation of objects to create objects. The. class file that he generated at compile time is: External class name $1.class.
Here are a few examples to illustrate the use of these:
public class Outerclass {
member Inner class
public class innerclass{
}
Static inner class--special case of members ' inner class
public static Class staticinnerclass{
}
public void Test () {
Local inner class
Class innerclass{
public void World () {
}
}
Innerclass inn = new Innerclass ();
Inn.world ();
Define a local inner class again
Class inner1class{
public void Holle () {
}
}
Inner1class inn1=new Inner1class ();
Inn1.holle ();
}
public void Test1 () {
Anonymous inner class
New Object () {
public void Hello () {
System.out.println ("Hello world!");
}
}.hello ();
}
}
public class Testmain {
public static void Main (string[] args) {
External class object
Outerclass out = new Outerclass (); Instantiating an external class object
member Inner class
Outerclass.innerclass in = Out.new innerclass (); Instantiating a member's inner class object
Static Inner class
Outerclass.staticinnerclass staticin = new Outerclass.staticinnerclass (); Instantiating a static inner class object
Local inner class
Out.test (); Because the local inner class simply instantiates the object directly within the method after the declaration is completed, you can call the
Anonymous inner class
Out.test1 ();
}
}