Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

a static inner class question

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic