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

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值