Java Member Inner class.

Java Member Inner Class.

  • A member inner class can bee defined inside a class the same way as a member field or a member method.
  • It can be declared as public, private, protected, or package-level.
  • The instance of a member inner class may exist only within the instance of its enclosing class.
  • A member inner class can also bee defined inside a method, but lives only as long the method executes.
  • A member inner class methods have access to all members of the class they are contained in.
    Member inner class example:
    public class InnerClassCar {
      String _color;                       // object of inner class country
      InnerClassCar(String col){
        _color=col;
       }
      class color {                           // member inner class
        String showColor() {
          return "My color is "+_color+"!";
        }
      }
      public String getColor() {
        color _color=new color();                // instantiate the inner class
        return _color.showColor();
      }
    
      public static void main(String[] args) {
        InnerClassCar myCar= new InnerClassCar("blue"); 
        // instantiated object of class InnerClassCar
        System.out.println(myCar.getColor());
      }
    }
    
    The result of this is:
    My color is blue!
    You can download this example here (needed tools can be found in the right menu on this page).
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.