Witscale Test Center

4.4 Non-static member class (aka member inner class)


4.4 Non-static member class (aka member inner class)

A non-static member class is member class without the keyword static in its declaration. Its declaration is directly enclosed inside a class or an interface declaration. It is declared as:

 

public class OuterOne {

      class InnerOne{

          }

}

 

When the above code is compiled, two files are generated, OuterOne.class and OuterOne$InnerOne.class. The member inner class is a member of enclosing class. Just like other members of a class, it can be declared as public, private, protected or default/friendly.

 

A top-level class cannot be declared as private or protected, but a member class can be.

 

Similar to private members of a class, a private member class is accessible only within the enclosing class. A protected member class is accessible to the enclosing class, all the subclasses of enclosing class and all the classes within same package as of the enclosing class. For example:

 

package com.example.scjp.studykit;

public class OuterOne {

      protected class InnerOne{

       }

}

 

The protected member inner class InnerOne will be accessible to OuterOne, all the subclasses of OuterOne and all the classes in package com.example.scjp.studykit.