Internal class, java internal class
The internal class is the class defined inside the class ......
Internal class access features
1. The internal class can directly access members of the external class, including private...
2. To access members of an internal class, the external class must create an object...
External class name. Internal class name object name = External Class Object. Internal class object;
Class Test {
Public static void main (String [] args ){
Outer. Inner oi = new Outer (). new Inner (); // create an internal Class Object
Oi. method ();
}
}
Class Outer {
Private int num = 10;
Class Inner {
Public void method (){
System. out. println (num );
}
}
}
/*
* A: internal class overview
* B: internal class access features
* A: Internal classes can directly access members of external classes, including private ones.
* B: To access members of an internal class, the external class must create an object.
* External class name. Internal class name object name = External Class Object. Internal class object;
* C: Case Study
* Internal class access features
*/
The above code, after compilation, generates Test in the directory where the source file is located. class Outer. class Outer $ Inner. class Three bytecode files. Inner is the internal class of the Outer class and is embedded in the Outer class...
The main functions of internal classes are:
1. The internal class provides better encapsulation, that is, better hiding details and better hiding. The internal class is hidden within the external class and cannot be accessed by other classes in the same package.
2. internal class members can directly access the private data of external classes, because internal classes are considered members of external classes, and members of the same class can access each other, however, external classes cannot access the implementation details of internal classes, such as member variables of internal classes... (To put it simply, internal access to the outside, external access to the inside)
3. The anonymous internal class applies to creating classes that only need to be used once...
Class Test {
Public static void main (String [] args ){
Outer. Inner oi = new Outer (). new Inner (); // create an internal Class Object
Oi. method ();
}
}
Class Outer {
Private int num = 10;
Class Inner {
Public void method (){
System. out. println (num );
}
}
}
External class Outer, internal class Inner, Inner this class is located inside the Outer class, not inside the member methods of the Outer class. If it is inside the member methods, it is called a local internal class, an internal class defined in a class that is not in the member method is called a member internal class. The member internal class is a member of the position and member variable, method, constructor, and initialization code block, the local internal class and anonymous internal class are not class members... the Inner class and num are both members of the Outer class...
There are two types of internal Member classes: static internal class and non-static internal class. The internal class acts as a member of the external class and can be modified using the public protected default private modifier like the member method member variable, the external class can only be modified by default and public... that is, the common public class and class
Members in a class can access each other... it is equivalent to a member method that can access member variables. The member internal class can access member methods and member variables. However, one member method cannot access the local variables of another member method. That is, the local variables of the two member methods cannot access each other, because the Member methods can be considered as class members, while the local variables of the member methods are inferior to those of the member methods, therefore, mutual access is not allowed. However, a member method must be able to access its own defined local variables, which is beyond doubt ......
Therefore, the statement System. out. println (num); the output is the num in the external class with a value of 10. In fact, there is also a proximity principle. However, because the Member's internal class does not have the num variable, so only the num variable of the output external class...
To call the method in the Test class, you must first create an object named Outer. inner oi = new Outer (). new Inner (); is the object that creates the Member's internal class...
Let's look at the following code.
Class Test {
Public static void main (String [] args ){
Outer o = new Outer ();
O. print ();
}
}
Class Outer {
Private int num = 10;
Private class Inner {
Public void method (){
System. out. println (num );
}
}
Public void print (){
Inner I = new Inner ();
I. method ();
}
}
This external class contains the member variable num, the member internal class Inner, the member method print, Outer o = new Outer (); is to create an Outer Class Object, o. print () is to use this object to call the Outer member method print. In the member method print, the operation is to create an Inner object and access its method...
Look at the static internal class...
Class Test {
Public static void main (String [] args ){
Outer. Inner oi = new Outer. Inner ();
Oi. method ();
Outer. Inner2.print ();
}
}
Class Outer {
Static class Inner {
Public void method (){
System. out. println ("method ");
}
}
Static class Inner2 {
Public static void print (){
System. out. println ("print ");
}
}
}
Here are two static member Internal classes. first look at the Inner internal class. The method of this class is non-static. Therefore, you need to call the Inner object, but Inner is a member of the Outer class, we regard this member as a whole, and this member is static. Therefore, the external class calls this member directly using the class name, so Outer. inner oi = new Outer. inner ();
Oi. method (); is to call the method, but for the print method, the class name is called directly...