This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner classes" Watch "Inner classes" New topic
Author

Inner classes

y sudhakar
Greenhorn

Joined: Mar 15, 2001
Posts: 1
Hai all,
I wrote one class and within that I wrote a inner class. If I make some of the variable in the inner class static the compiler is complaing that inner class should not contain static member.
I am not able to understand why it should not be like that.
Is there any body who can explain what is the exact meaning behind this.
Thanx
sudhakar
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
unless inner class is not static inner class you are not allowed to have static members in it. There is one thing you can do to have a member static is by making it final but remember it's not the same then because it's a constant.


Val SCJP <BR>going for SCJD
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

sudhakar,
The reason that constraint is placed on inner classes is this :
Unless an inner class is specified as static (which means it always exists), a non-static inner class needs an instance of the outer class to exist... Static attributes always exist, so how could a static attribute always exist if the class containing it ( the non-static inner class ) does not exist because it has no outer class to contain it?
HTH,
-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
y,
Please change your name to be compliant with JavaRanch's naming policy.
Your ID should be 2 separate names with more than 1 letter each. We really want this to be a proffesional forum and would prefer that you use your REAL name.
Thanks,
Cindy

"JavaRanch, where the deer and the Certified play" - David O'Meara
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inner classes
 
Similar Threads
Inner Class
Inner class doubt ?
static inner classes
About Inner Classes
Inner Class