Site Search:

Create inner classes including static inner class, local class, nested class, and anonymous inner class

<Back


http://xyzcode.blogspot.com/2017/01/nested-classes.html

Example:
What is the output of the following program?

public class OuterClass {
    
    private int x = 0;
    class InnerClass{
        private void show() {
            System.out.println(x);
            
            OuterClass o = new OuterClass();
            o.x = 3;
            
            class LocalClass{
                int y;
            }
            
            System.out.println(x + new LocalClass().y);
            System.out.println(o.x);
        }
    }
    
    void changeX() {
        this.x = 5;
    }
    
    static class StaticNestedClass{
        static private void show() {
            OuterClass o = new OuterClass();
            System.out.println(o.x);
        }
    }
    
    public static void main(String args[]) {
        OuterClass out = new OuterClass() {
            public void changeX() {
            }
        };
        out.changeX();
        OuterClass.InnerClass in = out.new InnerClass();
        in.show();
        
        OuterClass.StaticNestedClass.show();
    }
}


0
0
3
0