It's not a secret anymore!
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner classes" Watch "Inner classes" New topic

Inner classes

y sudhakar

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.
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

Joined: Oct 18, 2000
Posts: 4121

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?

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

Joined: Sep 29, 2000
Posts: 8521
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.

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: Inner classes
jQuery in Action, 3rd edition