Java Nested Class and Inner Class

This is based on the description of http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html 

and I have enclosed a example project to show some syntax and features related to the Nested Class.

 

In a short word, "the Java Programming Language allows you to define a classs within another class, such a class is called a nested class and .."

 

 

Terminology: Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes.

 

 

 

package com.boqwang.innner;

public class InnerClassTest {

    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner1 = createInnerClass(outer);
        OuterClass.InnerClass inner2 = createInnerClass(outer);
        OuterClass.InnerClass inner3 = outer.createInnerClass();
        
        
        boolean error = false;
        
        if (inner1.getOuterClass() != outer) {
            System.out.println("Error!");
            error = true;
        }
        if (inner2.getOuterClass() != outer) {
            System.out.println("Error!");
            error = true;
        }
        if (inner3.getOuterClass() != outer) {
            System.out.println("Error!");
            error = true;
        }
        
        if (inner1.getThis() != inner2.getThis() 
                && inner1.getThis() != inner2.getThis()
                && inner2.getThis() != inner3.getThis())  {
            System.out.println("Innner instances created by the same outer instance are different");
            
        }
        
        if (!error) {
            System.out.println("All inner instance created by the same outer class have the same reference to outer instance");
        }
    }
    
    /**
     * Create a Innert class instance
     * @param outerObject
     * @return inner class instance
     * It is required to have an outer class instance in order to create an inner class instance
     */
    public static OuterClass.InnerClass createInnerClass(OuterClass outerObject) {
        return outerObject.new InnerClass();
    }
}


class OuterClass {
    
    private int outerClassField = 1;
    
    private static int outerStaticClassField = 2;
    
    class InnerClass { 
        public int getOutClassField() {
            return OuterClass.this.outerClassField;
        }
        
        public OuterClass getOuterClass() {
            return OuterClass.this;
        }
        
        public InnerClass getThis() {
            return this;
        }
    }
    
    public InnerClass createInnerClass() {
        return new InnerClass();
    }
    
    static class NestedClass {
        
        public int getOuterClassStaticField() { 
            return OuterClass.outerStaticClassField;
        }
        
        // Nested static Class could not access outer class's instance field
        // because the Nested Class does not have the OuterClass.this pointer
//        public int getOuterClassField() { 
//            return OuterClass.this.outerClassField;
//        }
        
        // Nested static class does not have "this" pointer because it 
        // it is a static class
//        public NestedClass getThis() {
//            return this;
//        }
        
        // Nested static Class Does not have "Outer.this" pointer
        // because it is static
//        public OuterClass getThis() {
//            return OuterClass.this;
//        }
        
    }

}
 

 

 

So in a nutshell, 

 

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. The next figure illustrates this idea.

An Instance of InnerClass Exists Within an Instance of OuterClass.

 

An Instance of InnerClass Exists Within an Instance of OuterClass

 

 

 

 

 

 

 

你可能感兴趣的:(java,Class,nested/inner)