Java Member Classes
In this chapter you will learn:
- How to declare Nonstatic Member Classes
- Example - Java Member Classes
- How to use inner class as the underline data structure
- What is variable scope in terms of inner class
- Example - The inner class members are accessible only within the inner class
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 w ww. ja v a 2s . c om*/
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 . ja v a 2 s .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());
}
}
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 av a 2s.co m
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
//from w w w. ja v a2s .c o 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:
- How to declare and use Static Member Classes
- Example - Java Static Member Classes
- Example - implementation for different data types
Java Object
Java Object Reference Variable
Java Methods
Java Method Return
Java Method Parameters
Java Class Constructors
Java Default Constructor
Java Constructor Parameters
Java this Keyword
Java static keyword
Java Method Overload
Java Constructors Overload
Java Method Argument Passing
Java Method Recursion
Java Nested Class
Java Anonymous Classes
Java Local Classes
Java Member Classes
Java Static Member ClassesJava Class Variables
Java main() Method
Java Class Inheritance
Java super keyword
Java Method Overriding
Java Constructor in hierarchy
Polymorphism
Java final keyword
Java Abstract class
Java Class Access Control
Java Package
Java Packages Import
Java Interface
Java Interface as data type
Java interface as build block
Java instanceof operator
Java Source Files