alt
Advertisement
Sponsored links
Certification Series
Online Training
Career Series
Exforsys
Exforsys arrow Certification Series arrow SCJP arrow SCJP 5 : Chapter 1. Declarations, Initialization and Scoping (Part-6)
Site Search


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.


Trackback(0)
Comments (0)add comment

Write comment

busy
 
< Prev   Next >
Exforsys Offers
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape