wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Inner 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 "Static Inner Class" Watch "Static Inner Class" New topic

Static Inner Class

Vishal Arora

Joined: Jul 29, 2005
Posts: 9
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

Joined: Jun 29, 2005
Posts: 128
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 !!

<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
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

Joined: Jul 14, 2005
Posts: 187
For further clarity to Sherry's answer:

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

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

BTW, static members (other then nested classes) can be accesed from objects also. Consider following:
static String hello= "Hello"; //Define a static class member
(new Test038.Nested()).method(new Test038().hello); //1 Right.
[ August 07, 2005: Message edited by: Devender Thareja ]

Devender Thareja
It is sorta covered in the JavaRanch Style Guide.
subject: Static Inner Class
Similar Threads
about Sun Guoqiao's mock-exam-02-38
nested class method
static inner class
nested class
I have rewritten my mock exam and put it at http://www.jiris.com/