Nonstatic Member Classes

In this chapter you will learn:

  1. How to declare Nonstatic Member Classes
  2. How to use inner class as the underline data structure
  3. What is variable scope in terms of inner class

Syntax to declare Nonstatic Member Classes

A nonstatic member class is a non-static member of an enclosing class. Each instance of the nonstatic member class associates with an instance of the enclosing class.

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

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

class EnclosingClass {
  private int outerVariable;
// j  ava2  s  .  com
  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
  }
}

Use inner class as the underline data structure

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

class Item {/*from  j ava2s.  c  o  m*/
  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());
  }
}

Inner class and variable scope

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

class Outer {// ja v a2 s .  com
  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:

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
//from  j av  a2 s.co  m
    void display() {
      System.out.println("display: outer_x = " + outer_x);
    }
  }

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

When compiling the code above:

Next chapter...

What you will learn in the next chapter:

  1. How to define an anonymous class