• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static Inner Class

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code given below:

Can somebody let me know why there will be compile time error at 1 and 2 and why it will not be at 3.
I always get confused with instantiating these inner classes. I would really appreciate if somebody can help me out in this.


[ August 07, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Vishal,
Well here are the answers to ur queries:

static class Nested


U have declared the class Nested as an inner class alright...but u have also declared it as static.
A static class member does not need any object to be instantiated.

new Test038().new Nested().method("first"); //1



Above, u r instantiating the static class Nested by using the new operator (new Nested()) and hence the error at //1

new Test038().Nested.method("second"); //2



Above, u are again instantiating the base or parent class by using the new operator (new Test038()) and hence the error at //2

Test038.Nested.method("third"); //3



Above, u r using the correct way of accessing an inner static class from an outer class and hence no error at //3.

Hope this clears ur doubts !!

Tell me if I was wrong anywhere !!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sherry, I encourage you to take a little extra time to use the full english words "you", "your", "are" instead of the abreviations "u", "ur", "r". You have plenty of space here, and your posts will look more professional (useful if your next boss wants to get certified )

Thank you for your posts, they are much appreciated.
-Barry
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
Sherry, I encourage you to take a little extra time...



Hi Barry
Thanx alot 4 the suggession. I'd definitely take care next time. But the reason why I use the short forms is so that I can give the reply ASAP (As Soon As Possible ) before anyone else replies. And the reason is that I want 2 check my knowledge and speed of thought too !!

...and your posts will look more professional (useful if your next boss wants to get certified )



definitely I would like to be as professional as YOU want me to be because it will matter to my boss too.

Thank you for your advice again.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For further clarity to Sherry's answer:

<code>
new Test038().new Nested().method("first"); //1 Wrong
</code>

Here Object.new <Nested class> is not making any sense.
The rigth way to do it is:
<code>
(new Test038.Nested()).method("hello"); //1 Right.
</code>

BTW, static members (other then nested classes) can be accesed from objects also. Consider following:
<code>
static String hello= "Hello"; //Define a static class member
......
......
(new Test038.Nested()).method(new Test038().hello); //1 Right.
</code>
[ August 07, 2005: Message edited by: Devender Thareja ]
 
What? What, what, what? What what tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic