aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a static inner class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a static inner class question" Watch "a static inner class question" New topic
Author

a static inner class question

rainbow
Ranch Hand

Joined: Sep 10, 2000
Posts: 31
class NormalClass{
static class NestedClass{
}
}
public class test{
public static void main(String agrs[]){
test t = new test();
t.out();
}
void out(){
NormalClass c = new NormalClass();
NormalClass.NestedClass n = c.new NestedClass();
}
}
above code will compile and run correctly.but if I change the code to below:
class NormalClass{
static class NestedClass{
}
}
public class test{
public static void main(String agrs[]){
test t = new test();
t.out();
}
void out(){
//NormalClass c = new NormalClass();
NormalClass.NestedClass n = new NormalClass().new NestedClass();
}
}

it will compile without error,but run incorrectly.
who can explain it to me?Thanks a lot.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
In first case, c. New NestedClass is not a correct way of declaring an static inner class.
In second case, it will compile and run fine.
In face u can just declare
NormalClass.NestedClass n = new NormalClass.new NestedClass();
Hope this helps
-sampaths77
srikrish
Ranch Hand

Joined: Sep 12, 2000
Posts: 63

I compiled the code in the first case and ran it. It runs and compiles fine. In the second case, what happens is that the constructor for the NormalClass is not executed. I tested this by putting println statements inside the constructors for the NormalClass and NestedClass.
I'm not sure on the explanation for this but what I feel is that since NestedClass is a static member of NormalClass, when you try to instantiate a NestedClass object as done in case 2, it does not instantiate the parent object. I don't think you can say that it runs incorrectly in this case since you are not trying to create a NormalClass object.
sampaths77 - I got a compile error when I tried to compile the code with the foll stmt:
NormalClass.NestedClass n = new NormalClass.new NestedClass();
Srikrish
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
That's pretty serious...
I tried to compile question 4 from chapter 5 Bill Brodgen's Java 2 exam cram (page 94):
Which of the wollowing code fragments shows the correct way to declare and initialize a reference to a NestedClass object from outside of NormalClass:

answer given is:
NormalClass.NestedClass myNC = new NormalClass.new NestedClass();
but this caused compiler errormessage:

Srikrish: how did you run the second case? I tryed to run this:

and received error message: Exception in thread "main" java.lang.VerifyError: (class: test, method: out signature: (V() Excpecting to find uninitialized object on stack

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
angel custodio
Greenhorn

Joined: Aug 13, 2000
Posts: 19
Hi friends,
Mapraputa Is: The answer given on page 94 of Exam Cram is
NormalClass.NestedClass myNC = new NormalClass.NestedClass(), which is the correct way to get created a reference to a top-level static nested class --> read on pg.90.
But the second code you said, that's very interesting.

Could anybody explain it in detail?

Thanks to you all!
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
angel custodio:
Sorry, I did not understand... What is top-level static nested class? I thought a class can be either top-level, or nested. I will try to compile code on p.90, I wonder if it will work, but code from page 94 definitely doesn't.
angel custodio
Greenhorn

Joined: Aug 13, 2000
Posts: 19
Mapraputa Is:
As in Exam Cram pg.83 states: "Nested static class A named class declared static. It can directly access only static variables and methods. Considered a top-level class".
BTW, I've compiled code in pg.94, with the code of answer 'a' and worked for me. I've Standard JDK 1.2.2 on WNT 4.0 WS with SP3.
I didn't try the code in pg.90, but I can guess it will work.

Please correct me if I'm wrong.
Thanks in advance!

Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
angel custodio:
1. Thanks for the reference to p.83, I missed this point. But the whole sentence doesn�t make any sense for me. Why nested static class is considered a top-level class? It can be private (like inner classes), it cannot be instantiated without enclosing class instance (like inner classes), so for what purpose should we call it �top-level class�?
2. I also use jdk 1.2.2 (on Windows95), that�s very strange that the same code compile on your machine and doesn�t compile on my (and srikrish�). Could you post your program here? Maybe somebody else can try it�
srikrish
Ranch Hand

Joined: Sep 12, 2000
Posts: 63
mapraputa,
Nested static classes do not need an instance of the enclosing class for their construction.
In the above example, the NestedClass can be instatntiated by the following code:
class NormalClass
{
static class NestedClass
{
NestedClass ()
{
System.out.println ("nested");
}
}
NormalClass ()
{
System.out.println ("normal");
}
}
class test
{
public static void main(String agrs[])
{
test t = new test();
t.out();
}

void out()
{
NormalClass.NestedClass n =
new NormalClass.NestedClass();
}
}
This code compiles fine. When you run it, you will find that only "Nested" message is displayed indicating that the constructor for the NormalClass was not constructed.

[This message has been edited by srikrish (edited September 13, 2000).]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Thanks srikrish!
I analized your code and found my mistake:
Instead of:
NormalClass.NestedClass n = new NormalClass.NestedClass();
I typed:
NormalClass.NestedClass n = new NormalClass.new NestedClass();
(Actually I copied it from smbd message). But all discussion was useful: I will never forget how to instatiate static nested class . Also I learnt that my idea that top-level classes are not members of another class and cannot be private or protected was wrong. A class can be classificated as a top-level class if it can be instantiated without instance of enclosing class. Is this correct?
 
jQuery in Action, 2nd edition
 
subject: a static inner class question