Given a set of classes and superclasses, develop constructors for one or more of the classes. Given a class declaration, determine if a default constructor will be created, and if so, determine the behavior of that constructor. Given a nested or non-nested class listing, write code to instantiate the class.

Creating Inner Classes Instances

Non-static inner classes have a hidden reference to the enclosing class instance. This means you must have an instance of the enclosing class to create the inner class. You also have to use a special "new" function that correctly initializes the hidden reference to the enclosing class. The special "new" function is a member of the enclosing class.

public class InnerClassTest {	
	public class ReallyInner {	    
	}	
}
					
					
InnerClassTest o = new InnerClassTest();
InnerClassTest.ReallyInner i = o.new ReallyInner();
					
or
InnerClassTest.ReallyInner i = new InnerClassTest().new ReallyInner();
					

Example:

public class InnerClassTest {
	public void foo() {
	        System.out.println("Outer class");
	}
	
	public class ReallyInner {
	    public void foo() {
	        System.out.println("Inner class");
	    }
	
	    public void test() {
	        this.foo();
	        InnerClassTest.this.foo();
	    }
	}
	
	public static void main(String[] args) {        
	        InnerClassTest.ReallyInner i = new InnerClassTest().new ReallyInner();
	        i.test();
	}
}
					
					
Output:
Inner class
Outer class
					

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide