java inner class

java inner class

1. Put the definition of one class inside the definition of another class, this is the inner class.
2. The inner class can freely use the member variables of the outer class (including private ones) without generating the object of the outer class, which is the only advantage of the inner class.
3. There must be an object of the outer class before the object of the inner class can be generated, because the function of the inner class is to access the member variables in the outer class.
4. When there are no member variables and local variables of the same name in the inner class, the inner class will directly access the member variables of the outer class without specifying the Out.this. property name. Otherwise, the local variables in the inner class will overwrite the outer class's member variables. Member variables.
5. Accessing the member variables of the inner class itself can use the this. attribute name, and accessing the member variables of the outer class needs to use the Out.this. attribute name.
6. If you use static to make the inner inner static, then the inner class can only access the static member variables of the outer class, which has
limitations The object of the inner class (you don't need to new out the outer class object and then new out the inner class object)
8. If an inner class only wants to be operated by the methods in the outer class, you can use private to declare the inner class
9. Only the outer class can be used for private inner classes. Control it
10. The final definition must be used when passing parameters to the inner class of the method (final has no special meaning here, it is just a representation)
11. Member inner class is a member of the outer class, which can directly use the outer class All members and methods, even private ones. At the same time, if the outer class wants to access all member variables/methods of the inner class, it needs to be obtained through the object of the inner class.
12. Member inner classes (except static ones) cannot contain static variables and methods. (The java virtual machine requires that all static variables must be completed before the object is created)



Example 1:
//external class
class Out {
    private int age = 12;

    //inner class
    class In {
        public void print() {
            System.out.println(age);
        }
    }
}

public class Demo {
    public static void main(String[] args) {
        Out.In in = new Out().new In();
        in.print();
        //or access in the following way
        /*
        Out out = new Out();
        Out.In in = out.new In();
        in.print();
        */
    }
}


Analysis:
1. After the program is compiled, two .class files will be generated, namely Out.class and Out$In.class ($ means In is inside Out)
2.Out.In in = new Out().new In( ) can be used to generate objects of inner classes. There are two small points of knowledge to pay attention to in this method
(1). The Out at the beginning is to indicate which outer class the inner class object needs to be generated in
(2). There must be an external class first. The object of the class can generate the object of the inner class, because the function of the inner class is to access the member variables in the


outer class. The variable access form in the inner class
class Out {
    private int age = 12;

    class In {
        private int age = 13;
        public void print() {
            int age = 14;
            System.out.println("Local variable: " + age);//14
            System.out.println("Internal class variable: " + this.age);//13
            System.out.println("External class variable: " + Out.this.age);//12
        }
    }
}

public class Demo {
    public static void main(String[] args) {
        Out.In in = new Out().new In();
        in.print();
    }
}



static inner class
class Out {
    private static int age = 12;

    static class In {
        public void print() {
            System.out.println(age);
        }
    }
}

public class Demo {
    public static void main(String[] args) {
        Out.In in = new Out.In();
        in.print();
    }
}


Analysis:
1. If the inner class is made static with static, then the inner class can only access the static member variables of the outer class, which has limitations
. 2. Because the inner class is static, Out.In can be viewed as a whole. Directly new out the object of the inner class (access static through the class name, it doesn't matter whether the outer class object is generated or not)


private inner class
class Out {
    private int age = 12;

    private class In {
        public void print() {
            System.out.println(age);
        }
    }
    public void outPrint() {
        new In().print();
    }
}

public class Demo {
    public static void main(String[] args) {
        //this method is invalid
        /*
        Out.In in = new Out().new In();
        in.print();
        */
        Out out = new Out();
        out.outPrint();
    }
}


Analysis:
1. If an inner class only wants to be operated by the methods in the outer class, then you can use private to declare the inner class
2. The private inner class can only be controlled by the outer class


method inner class
class Out {
    private int age = 12;

    public void Print(final int x) {
        class In {
            public void inPrint() {
                System.out.println(x);
                System.out.println(age);
            }
        }
        new In().inPrint();
    }
}

public class Demo {
    public static void main(String[] args) {
        Out out = new Out();
        out.Print(3);
    }
}


Analysis:
1. The inner class of the method, if we need to pass parameters into the method of the outer class at this time, then the method parameter of the outer class must be defined using final 2. Final
has no special meaning here, it is just a representation
3. The final definition must be used when passing formal parameters within a method (final has no special meaning here, it is just a representation)



refer to the original text: http://www.cnblogs.com/nerxious/archive/2013/01/ 24/2875649.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326982640&siteId=291194637