File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's exam nested class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's exam nested class

Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39


From the static context of class Z, the instance fields and methods of the enclosing class B can be accessed only if the simple name of the instance field or method is qualified with a reference to a specific instance of class B. Suppose a reference variable r1 refers to an instance of the enclosing class B. Then the instance member s2 of the enclosing class instance referenced by r1 could be accessed using the expression r1.s2.
My question is :in the above problem where exactly can u create and instance of class B and use that reference to the instance to print the value of instance variable s2 in line 1?
If this is too silly a question please excuse me.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Are you askiing about the scope of declaration or recursive call issues? Please refine your question.
Sumitro Palit
Ranch Hand

Joined: Dec 13, 2003
Posts: 37
Sagarika,
At line 1, ie inside the Z constructor scope, Z can access s1, s3, s4, s5 & s6. But not s2 directly becasue it is not static. Remember that the static inner class can only access static members of the outer class as it doesn't need an instance of the outer class to exist
As Vad pointed out, if you try to instantiate a new B inside of Z, you'll run into recursive issues as the B constructor instantiates a new Z.
You can modify the Z constructor to accept an additional B parameter and then pass a B reference to the Z constructor (thereby explicitly passing an outer reference to the static inner class):
class B {
private static String s1 = "s1";
final String s2 = "s2";
B () { new Z("s5","s6", this); }
static class Z {
final String s3 = "s3";
static String s4 = "s4";

// forcibly passing a reference to the outer class
Z (final String s5, String s6, B b) {
System.out.print(b.s2);//line 1
}
}

public static void main(String args[]) {
B ob=new B();
}
}
Hope this helps,
- ortimuS
[ December 14, 2003: Message edited by: ortimus tilap ]
Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39
Ok!
That was cool!It never struck me.I was having the recursion issues and was confused.I guess my brain also underwent some recursion and froze for sometime.Thank you Ortimus.That was indeed what I was trying to do.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan's exam nested class
 
Similar Threads
static inner classes
nested class Danchisholm ques.
Strings, StringBuffer and equals()
Local class program error
nested class Danchisholm ques.