Java Member Classes

Description

A member class is a member of an enclosing class. Each instance of the member class associates with an instance of the enclosing class.

The member class's instance methods can call instance methods in the enclosing class and access the enclosing class instance's nonstatic fields.

Example

The following code has one outer class named EnclosingClass and a nonstatic member class named EnclosedClass.


class EnclosingClass {
  private int outerVariable;
/*from   ww  w. j  a v a 2  s  .  c o m*/
  private void privateOuterMethod() {
    System.out.println(outerVariable);
  }

  class EnclosedClass {
    void accessEnclosingClass() {
      outerVariable = 1;
      privateOuterMethod();
    }
  }
}

public class Main {
  public static void main(String[] args) {
    EnclosingClass ec = new EnclosingClass();
    ec.new EnclosedClass().accessEnclosingClass(); // Output: 1
  }
}

The code above generates the following result.

Example 2

The following code uses inner class ItemList to store the items.


class Item {/*from w  w  w .jav a2s. com*/
  private String name;
  private String desc;

  Item(String name, String desc) {
    this.name = name;
    this.desc = desc;
  }

  String getName() {
    return name;
  }

  String getDesc() {
    return desc;
  }

  @Override
  public String toString() {
    return "Name = " + getName() + ", Desc = " + getDesc();
  }
}

class ItemManager {
  private ItemList itemList;
  private int index = 0;

  ItemManager() {
    itemList = new ItemList(2);
  }

  boolean hasMoreElements() {
    return index < itemList.size();
  }

  Item nextElement() {
    return itemList.get(index++);
  }

  void add(Item item) {
    itemList.add(item);
  }

  private class ItemList {
    private Item[] itemArray;
    private int index = 0;

    ItemList(int initSize) {
      itemArray = new Item[initSize];
    }

    void add(Item item) {
      if (index >= itemArray.length) {
        Item[] temp = new Item[itemArray.length * 2];
        for (int i = 0; i < itemArray.length; i++)
          temp[i] = itemArray[i];
        itemArray = temp;
      }
      itemArray[index++] = item;
    }

    Item get(int i) {
      return itemArray[i];
    }

    int size() {
      return index;
    }
  }
}

public class Main {
  public static void main(String[] args) {
    ItemManager itemManager = new ItemManager();
    itemManager.add(new Item("1", "A"));
    itemManager.add(new Item("2", "B"));
    itemManager.add(new Item("3", "C"));
    while (itemManager.hasMoreElements())
      System.out.println(itemManager.nextElement());
  }
}

The code above generates the following result.

Example 3

The following program illustrates how to define and use an inner class.

 
class Outer {//from w  w  w. j  a  va 2  s . c  om
  int outer_x = 100;
  void test() {
    Inner inner = new Inner();
    inner.display();
  }
  class Inner {
    void display() {
      System.out.println("display: outer_x = " + outer_x);
    }
  }
}
public class Main {
  public static void main(String args[]) {
    Outer outer = new Outer();
    outer.test();
  }
}

Output from this application is shown here:

Example 4

The inner class members are accessible only within the inner class and may not be used by the outer class. If you try to compile the following code, you will get error message.

 
public class Main {
  int outer_x = 100;
  // this is an inner class
  class Inner {
    int y = 10; // y is local to Inner
//w  w  w.  jav a  2  s .  com
    void display() {
      System.out.println("display: outer_x = " + outer_x);
    }
  }

  void showy() {
    System.out.println(y); 
  }
}

When compiling the code above:





















Home »
  Java Tutorial »
    Java Language »




Java Data Type, Operator
Java Statement
Java Class
Java Array
Java Exception Handling
Java Annotations
Java Generics
Java Data Structures