Java static inner classes, members of the inner class Detailed

External and internal class Class

As we all know, each file must have an external java class with the java file of the same name, this external class can be public or default of. In addition to the external class with the same name, you may have the same name as the class with other external flat outer class level, but they must be the default.

And then define inner class refers to a class inside the outer class.

Classification inner class

We are talking about inner classes, it is the official name for nested class (Nested Classes). Nested class including a static inner classes (Static Nested Classes) and an internal class (Inner Classes). The interior is divided into categories member inner classes, local inner classes (Local Classes) and anonymous inner classes (Anonymous Classes).

Here Insert Picture Description
From the use of terms, it should be static inner classes and members of inner classes grouped together because both are through outer.staticInneror outer.memberInnerthis .syntax to use the class name. In contrast, partial inner and anonymous inner classes can not pass .to the class name syntax, because the use of both will be limited to within a scope of (i.e. in a pair {...}among braces), this is because the two are not as members of the outer class (neither a static member, nor is it an instance member).

  • Non-static inner classes can not have static members and static methods . Static properties do not depend on the object as it is stored in a static area jvm, so there is no need to access the live objects; rather than static inner classes is also equivalent to non-static properties of the outer class, and its performance is in line with non-static ( require an external class object to create an internal class object). If a non-static inner class with static properties, static properties and do not rely on any internal instance of the class, it would destroy the semantics of non-static inner classes.
  • And on a relative, static inner class can have, static members, static methods, static inner classes .
  • Static inner classes to create objects, citing the need for external class object .
  • From the object static inner class, can not access to the external object, i.e., you can not access the external members and methods of the class object . This is because there is no reference to the outer class object.

Constructor

class MNA {
    private void f() {}
    private static void ff(){}
    class A {//成员内部类
        private void g() {}
        public class B {
            void h() {
                g();
                f();
                ff();
            }
        }
    }
}

class outer{
    void o1(){}
    private static void ff(){}
    static class staticInner{//静态内部类
        void i1(){
            //o1();无法通过编译
            ff();
        }
    }
}

public class MultiNestingAccess {
    public static void main(String[] args) {
        MNA mna = new MNA();
        MNA.A mnaa = mna.new A();
        MNA.A.B mnaab = mnaa.new B();
        mnaab.h();

        outer.staticInner sInner = new outer.staticInner();
    }
}
  • Like normal constructor static inner classes, but need to use .grammar to point out static inner classes.
  • The members within the class constructor is, outerInstance.new inner()that 外部类对象.new 内部类().
  • Members of the inner class can freely access the non-static method or member of the outer class (because they have a reference to the outer class object); and a static inner classes can not.
  • The static methods or member of the outer class for members of the inner class and static inner class, you can access. Because no need to refer to the outer class object.
Published 171 original articles · won praise 130 · views 280 000 +

Guess you like

Origin blog.csdn.net/anlian523/article/details/99877720