I. overview
static modifies the meaning of inner classes:
1. Ordinary internal classes generally have an implicit reference to external classes. If the internal class is modified with static, this relationship will disappear, and nested classes will not need to be created through external reference instances;
2. Non static peripheral class members cannot be accessed from nested classes;
3. Fields and methods of ordinary inner classes can only be placed on the outer layer of the class, that is, ordinary inner classes cannot contain static data (fields, methods and nested classes), while nested classes can contain static data.
2, Code description
1. Public interface
Contents.java
package com.lh.innerclass.class4; public interface Contents { int value(); }
Destination.java
package com.lh.innerclass.class4; public interface Destination { String readLabel(); }
2. Nested class test
package com.lh.innerclass.class4; public class Parcel { private static class ParcelContents implements Contents{ private int i=11; public int value(){ return this.i; } } //Static members are not allowed in ordinary inner classes!!! protected static class ParcelDestination implements Destination{ private String label; //Constructors of inner classes are generally set to private private ParcelDestination(String whereTo){ this.label = whereTo; } private static int x = 10; public String readLabel(){ return this.label; } static void f(){ System.out.println("ParcelDestination.f()"); } //There can be nested classes in nested classes static class AnotherLevel{ private static int x = 10; static void f(){ System.out.println("ParcelDestination.AnotherLevel.f()"); } } //Nested classes can also have common inner classes private class InnerClass{ private int x = 10; //Static fields cannot be defined in a normal inner class // private static String aa = "a"; private String a; void f(){ System.out.println("ParcelDestination.InnerClass.f()"); } //Static methods cannot be defined in a normal inner class // static void ff(){ // System.out.println("ParcelDestination.InnerClass.f()"); // } } } static Contents contents(){ return new ParcelContents(); } static Destination destination(String s){ return new ParcelDestination(s); } public static void main(String[] args) { Parcel.contents(); Parcel.destination("Tasmania"); } }
3. Interface internal class test ClassInInterface.java
package com.lh.innerclass.class4; /**** * * Internal class of interface: the default nested class with static keyword! * * @author Liu * */ public interface ClassInInterface { void howdy(); //Default nested class with static keyword! class Test implements ClassInInterface{ public void howdy() { System.out.println("howdy!"); } public static void main(String[] args) { new Test().howdy(); } } }
Note: the nested classes defined in the interface are public by default with the keyword static
4. Multi nesting access.java
package com.lh.innerclass.class4; /**** * * Accessing members of external classes from multiple nested classes * Meaning of nested class: ordinary inner class * * @author Liu * */ class MNA{ void f(){ System.out.println("MNA.f()"); } class A{ void g(){ System.out.println("MNA.A.g()"); } class B{ void h(){ System.out.println("MNA.A.B.h()"); A.this.g(); MNA.this.f(); } } } } public class MultiNestingAccess { public static void main(String[] args) { new MNA().new A().new B().h(); } }