Java Reference
In-Depth Information
// Inner class starts here
public class Inner {
// Instance variable for Inner class
private int value = 1720;
public void printValue() {
System.out.println("\nInner - printValue()...");
System.out.println("Inner: Value = " + value);
System.out.println("Outer: Value = " + ModifiedOuter2.this.value);
}
} // Inner class ends here
// Instance method for ModifiedOuter2 class
public void printValue() {
System.out.println("\nOuter - printValue()...");
System.out.println("Outer: Value = " + value);
}
// Another instance method for the ModifiedOuter2 class
public void setValue(int newValue) {
System.out.println("\nSetting Outer's value to " + newValue);
this.value = newValue;
}
}
Listing 2-20. Testing the ModifiedOuter2 Class
// ModifiedOuter2Test.java
package com.jdojo.innerclasses;
public class ModifiedOuter2Test {
public static void main(String[] args) {
ModifiedOuter2 out = new ModifiedOuter2();
ModifiedOuter2.Inner in = out.new Inner();
// Print the value
out.printValue();
in.printValue();
// Set a new value
out.setValue(828);
// Print the value
out.printValue();
in.printValue();
}
}
 
Search WWH ::




Custom Search