Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Inner Class

 
Vishal Arora
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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 ]
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • 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 !!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Devender Thareja
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic