aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner classes Problem 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 "Inner classes Problem" Watch "Inner classes Problem" New topic
Author

Inner classes Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634




getting error:

inner classes cannot have static declarations
static void Bunkamethod1()
^
1 error


why are inner classes not allowed static declaration ??


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Lets look at this way, the inner class is a member ( instance) variable (assume like that not the same) for the outer class. So it is like if you are declaring the instance methos in a class. Now think, can you declare a static variable inside the method?
So just like inside the instance declaration of a inner class you cannot declare static variables.
If you want to, declare inner class as static.


SCJP 1.6 96%
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

We appreciate the static members because they are available to us as ready to use without creating the new instance of the of the enclosing class i.e without the headache of "new"

Let assume we have class called "Outer" and which has non static inner class called "Inner".
Is there way to access the members of "Inner" class without crating the instance of Outer class ?
No! we can't access them.

If we can't access the members of "Inner" class without creating instance of "Outer" class. then what is the use if we have the static member in "Inner" class. hence compiler won't allow us have static members in non static inner class.

I hope that helps.
 
jQuery in Action, 2nd edition
 
subject: Inner classes Problem