Exforsys.com
 
Home Certification SCJP
 

SCJP 5 : Chapter 1. Declarations, Initialization and Scoping (Part-6)

 

SCJP 5 : Chapter 1. Declarations, Initialization and Scoping (Part-6)

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


______________


Author: Mikalai Zaikin. Please Click Here to visit Authors site for any updates and changes to the study notes.



Read Next: SCJP 5 : Chapter 2. Flow Control (Part-2)



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape