Static non-static nested classes and nested classes

 What is a nested class and inner class? 

In another class can be defined inside a class, this class is called nested classes (nested classes), which has two types: 
static and non-static nested classes nested class static nested classes rarely used, the most important the non-static nested classes, that is, referred to as
inner classes (inner) nested class, introduced in JDK1.1 wherein inner class can be divided into three kinds: 
internal (1) in a class defined directly the like;
(2) inside a class defined in the method;
(3) anonymous inner classes.
 
1. Static nested classes

static modifier such nested class object a static member outside the class, directly associated with the external class.

Such static nested class as a static member can access only static members outside of class, because the external class non-static member associated with the external object, a static nested class can not access them, which makes the function of the static nested class becomes very weak, we can place very little.

Moreover, because the static nested class is not attached to the outer class external object, the external object so that different share a static nested class, which is different from the internal class can be used to package the main method.

Example declaration of a static nested class:

OuterClass.StaticNestedClass nestedObject =
     new OuterClass.StaticNestedClass();

Code is defined as follows as a static nested class

Inner Package; 
public class StaticTest { 
   Private static String name = "woobo"; 
   Private String NUM = "X001"; 
   static the Person class 
   { 
     Private address String = "China"; 
     public mail String = "[email protected]"; // public members of the inner class 
     public void Run the display () { 
       //System.out.println(num);// can not directly access non-static members of the outer class 
       System.out.println (name); // only direct access to the outside static members of a class 
       System.out.println ( "Inner" + address) ; // access to the internal class members. 
     } 
   } 
   Public void printInfo () { 
     the Person Person = new new the Person (); 
     person.display (); 
     //System.out.println(mail);// inaccessible 
     //System.out.
     System.out.println (person.address); // can access the private members of class inside 
     System.out.println (person.mail); // public member can be accessed within the class 
   } 
   public static void main (String [] args ) { 
     StaticTest StaticTest new new StaticTest = (); 
     staticTest.printInfo (); 
   } 
}

  

2. Internal class

All members do not have a static modification means that internal and external association class is a class object, it can be said that an internal class objects exist outside the class object is a member of the outer class object, so inner class object can access the outer class object , including private members.

  • Because the inner class depends on the external object exists, you can not define any static members.
  • Inner class object can access all member variables outside of class, including private members, it is the principle of Java closures;
  • Because the inner class implicit reference to the outer class, outside of class so it can not be automatic garbage collection garbage collection JVM.
  • No public inner class objects between different members of the outer class object.

Statement inner class example:

OuterClass.InnerClass innerObject = new OuterObject().new InnerClass();

  

3. Variable shadowing Shadowing

Nested classes and class variables, and local methods encapsulating scope overlapping area, the same name if the variable is masked occur.

  

public class ShadowTest {

    public int x = 0;

    class FirstLevel {

        public int x = 1;

        void methodInFirstLevel(int x) {
            System.out.println("x = " + x);
            System.out.println("this.x = " + this.x);
            System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
        }
    }

    public static void main(String... args) {
        ShadowTest st = new ShadowTest();
        ShadowTest.FirstLevel fl = st.new FirstLevel();
        fl.methodInFirstLevel(23);
    }
}

  

Note that reference to three different variables, the following results

 

x = 23 // 1. Local variables 
this.x = 1 // 2. Internal class variable 
ShadowTest.this.x = 0 // 3. External class variable

This inner class pointer to its own internal class, ShadowTest.this points to external object;

Without modification variables, the nearest matching principle will be performed; if the same variable name shadowing effect will occur; should be hidden in order to prevent internal class reference external object using the third method.

 

 

 

Guess you like

Origin www.cnblogs.com/wzdnwyyu/p/11095667.html