Deep understanding of java Nested classes and internal classes, and deep understanding of java Nested classes
1. What are nested classes and internal classes?
You can define another class within a class. This class is called the nested classes. It has two types: static nested class and non-static nested class. Static Nested classes are rarely used. The most important is non-static Nested classes, that is, they are called internal classes (inner ). Nested classes are introduced from JDK1.1. The inner class can be divided into three types:
1. Internal classes directly defined in a class (external class;
2. Internal classes defined in a method (external class method;
Third, anonymous internal class.
The following describes the usage and precautions of these Nested classes.
Ii. Static nesting
The following code defines a static nested class,
- Public class StaticTest {
- Private static String name = "javaJohn ";
- Private String id = "X001 ";
- Static class Person {
- Private String address = "swjtu, chenDu, China ";
- Public String mail = "josserchai@yahoo.com"; // internal class public Member
- Public void display (){
- // System. out. println (id); // you cannot directly access non-static members of an external class.
- System. out. println (name); // only the static members of the external class can be accessed directly.
- System. out. println ("Inner" + address); // access the internal class member.
- }
- }
- Public void printInfo (){
- Person person = new Person ();
- Person. display ();
- // System. out. println (mail); // inaccessible
- // System. out. println (address); // inaccessible
- System. out. println (person. address); // you can access private members of an internal class.
- System. out. println (person. mail); // you can access public members of the internal class.
- }
- Public static void main (String [] args ){
- StaticTest staticTest = new StaticTest ();
- StaticTest. printInfo ();
- }
- }
In a static nested class, you cannot access non-static members of the external class, which is limited by "static methods cannot access non-static members directly" in Java syntax. To access external class variables, you must use other methods. For this reason, static Nested classes are rarely used. Note: The members of the external class access internal class are special and cannot be accessed directly, but they can be accessed through the internal class. This is because all the Members and methods in the static nesting are considered static. Note that the internal static class Person is only visible within the StaticTest class. If it is referenced or initialized in other classes, it is incorrect.
3. Define internal classes in external classes
The following code defines two internal classes and their call relationships for external classes:
- Public class Outer {
- Int outer_x = 100;
- Class Inner {
- Public int y = 10;
- Private int z = 9;
- Int m = 5;
- Public void display (){
- System. out. println ("display outer_x:" + outer_x );
- }
- Private void display2 (){
- System. out. println ("display outer_x:" + outer_x );
- }
- }
- Void test (){
- Inner inner = new Inner ();
- Inner. display ();
- Inner. display2 ();
- // System. out. println ("Inner y:" + y); // internal variables cannot be accessed.
- System. out. println ("Inner y:" + inner. y); // you can access
- System. out. println ("Inner z:" + inner. z); // accessible
- System. out. println ("Inner m:" + inner. m); // accessible
- InnerTwo innerTwo = new InnerTwo ();
- InnerTwo. show ();
- }
- Class InnerTwo {
- Inner innerx = new Inner ();
- Public void show (){
- // System. out. println (y); // The Innter's y member cannot be accessed.
- // System. out. println (Inner. y); // you cannot directly access any member or method of Inner.
- Innerx. display (); // accessible
- Innerx. display2 (); // accessible
- System. out. println (innerx. y); // accessible
- System. out. println (innerx. z); // accessible
- System. out. println (innerx. m); // accessible
- }
- }
- Public static void main (String args []) {
- Outer outer = new Outer ();
- Outer. test ();
- }
- }
The above code must be noted that for internal classes, public or private delimiters are usually not added before the class Keywords of the class are defined. If the class keyword is added, at the same time, it seems that these qualifiers have no influence on the internal class variables and methods (?). In addition, it should be noted that the Inner and InnterTwo of the internal class are only known within the scope of the class Outer. If any code outside the class Outer tries to initialize the class Inner or use it, compilation fails. At the same time, the variable members of the internal class can only be seen inside the internal. If the external class or the internal class at the same level needs to be accessed, the method in the sample program should be used, and the variables of the internal class cannot be directly accessed.
Applications of JAVA internal and Nested classes
Class {
Class Ainner {
Class Inner {
}
}
}
Do you mean this?
JAVA internal class nesting
First of all, the dynamic nesting of ABC is certainly suspected of serious misuse ..
If you want to forcibly reference A member in an instance object, write it as A. this. methodOfA ();
That is
Public void fun (){
System. out. println (A. this. propertyX );
A. this. methodOfA ();
}
If the inner class does not change with the content of the Instance Object of the outer class, the inner class must be added with static class C as the static class. This reduces unnecessary resource waste by an order of magnitude.
If the inner class is public, it is called by other classes, or the logical subordination is not obvious. consider not writing it into the inner class. Write a java file separately.