static static inner class static inner classes

Static inner classes

Static inner classes:

 

1, with a modified internal static class called static inner class, entirely outside the class itself, does not belong to an external object class

 

2, the role of the static keyword is modified to become a member of the relevant class instead of an instance related

 

3, static inner class can contain static members, may also contain non-static member, but can not declare static members in a non-static inner classes.

 

4, the internal static class instance members are not accessible outside the class, class members can only access the outer class, even if the method is an example of static inner class can not access instance members of the outer class, can only access static members outside of class

 

5, external static class can not be defined as a class, Java static class has only one, that is static inner class, the top class can not be modified by static

6, how to call external class properties and methods of static inner classes

   1) External non-static classes can call the static properties and methods within the class by the method of creating the internal static class instance

   2) external class directly through the "external type internal class. Property (Method)" method call static properties and methods static inner classes directly

7, how to create a static inner class instance

   1) In the non-class external: internal external name class class name name = new class name inside the outer class name ();.

   2) in the outer class: class name name = new internal inner class name ();

 

Code examples:

Copy the code
{class StaticDome01 public 
    Private int A = 10; // Instance members 
    private static int b = 20; // class members 

    static class StaticClass { 
        public static int = 30 C; 
        public int D = 40; 
        // Class Methods 
        public static void print () { 
            // The following code error, a static class can not access the internal members of the class instance external 
            //System.out.println(a); 
     
            // static inner class can only access the external members of the classes 
            System.out.println ( "static internal static methods class, calling an external member classes B "); 
            System.out.println (" outer class = class member B "+ B); 
            
        } 
        Private void Syso () { 
            // the TODO Auto-Generated method Stub 

        } 
        / / method examples 
        public void print01 () {
            The method of class within which the static inner // Invoke instance method static inner class, belonging to the class instance method called external static inner class 
            System.out.println ( "Method static inner class instance, call the static inner classes examples of the method "); 
            StaticClass new new StaticClass SC = (); 
            sc.print (); 
        } 
        
    } 
}
Copy the code

Test categories:

Copy the code
class Text {public 
    public static void main (String [] args) { 
        / * 
         * To access an instance member static inner class, you must first create an instance of an object, you can call, because instance members belong to instances, create a method: External class name. internal class name name = new new 
         * outside the class name. internal class name () 
         * the "outer class internal class. property (method)" is directly invoked static inner class static properties and methods 
         * / 
        StaticDome01. StaticClass st = new StaticDome01.StaticClass (); 
        example internal static method // access class 
        st.print01 (); 
        example // access a static member within the class 
        int I = st.d; 
        System.out.println ( "no external examples of members of the class called static inner class d = "+ i); 
        class member // access static inner classes 
        ; int j = StaticDome01.StaticClass.c 
        class members no external static inner class called class System.out.println (" C = "+ j); 
        class method access static inner class // 
        System.out.println ( "class method access static inner classes:");
        StaticDome01.StaticClass.print (); 
    } 
}
Copy the code

operation result:

Static inner classes:

 

1, with a modified internal static class called static inner class, entirely outside the class itself, does not belong to an external object class

 

2, the role of the static keyword is modified to become a member of the relevant class instead of an instance related

 

3, static inner class can contain static members, may also contain non-static member, but can not declare static members in a non-static inner classes.

 

4, the internal static class instance members are not accessible outside the class, class members can only access the outer class, even if the method is an example of static inner class can not access instance members of the outer class, can only access static members outside of class

 

5, external static class can not be defined as a class, Java static class has only one, that is static inner class, the top class can not be modified by static

6, how to call external class properties and methods of static inner classes

   1) External non-static classes can call the static properties and methods within the class by the method of creating the internal static class instance

   2) external class directly through the "external type internal class. Property (Method)" method call static properties and methods static inner classes directly

7, how to create a static inner class instance

   1) In the non-class external: internal external name class class name name = new class name inside the outer class name ();.

   2) in the outer class: class name name = new internal inner class name ();

 

Code examples:

Copy the code
{class StaticDome01 public 
    Private int A = 10; // Instance members 
    private static int b = 20; // class members 

    static class StaticClass { 
        public static int = 30 C; 
        public int D = 40; 
        // Class Methods 
        public static void print () { 
            // The following code error, a static class can not access the internal members of the class instance external 
            //System.out.println(a); 
     
            // static inner class can only access the external members of the classes 
            System.out.println ( "static internal static methods class, calling an external member classes B "); 
            System.out.println (" outer class = class member B "+ B); 
            
        } 
        Private void Syso () { 
            // the TODO Auto-Generated method Stub 

        } 
        / / method examples 
        public void print01 () {
            The method of class within which the static inner // Invoke instance method static inner class, belonging to the class instance method called external static inner class 
            System.out.println ( "Method static inner class instance, call the static inner classes examples of the method "); 
            StaticClass new new StaticClass SC = (); 
            sc.print (); 
        } 
        
    } 
}
Copy the code

Test categories:

Copy the code
class Text {public 
    public static void main (String [] args) { 
        / * 
         * To access an instance member static inner class, you must first create an instance of an object, you can call, because instance members belong to instances, create a method: External class name. internal class name name = new new 
         * outside the class name. internal class name () 
         * the "outer class internal class. property (method)" is directly invoked static inner class static properties and methods 
         * / 
        StaticDome01. StaticClass st = new StaticDome01.StaticClass (); 
        example internal static method // access class 
        st.print01 (); 
        example // access a static member within the class 
        int I = st.d; 
        System.out.println ( "no external examples of members of the class called static inner class d = "+ i); 
        class member // access static inner classes 
        ; int j = StaticDome01.StaticClass.c 
        class members no external static inner class called class System.out.println (" C = "+ j); 
        class method access static inner class //
        ( 'Class method access static inner classes: ") System.out.println; 
        StaticDome01.StaticClass.print (); 
    } 
}
Copy the code

operation result:

Guess you like

Origin www.cnblogs.com/bichen-01/p/11198753.html