wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inner class" Watch "inner class" New topic
Author

inner class

Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
Can a member inner class be declared protected, as well as private, public or the default? I understand that both local and anonymous inner classes are not allowed access modifers. Thanks.
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
Have you tried it?
Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
Yeah..I used this code:
public class Test5 {
public static void main(String args[]) {
Test5 o = new Test5();
Test5.Inner i = o.new Inner();
i.print();
}
protected class Inner {
public static void main(String args[]) {
System.out.println("It works");
}
}
} //end of class Test5

Apparently, it works. Just another thing I want ironed out. Can I declare a static method body in an inner class? The reason is because when I tried changing the print() {...} method above to the "public static void main(String args[]) {...}" it threw me an error message.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Only static inner classes can have static methods... for all others it is not allowed...


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

Joined: Sep 29, 2000
Posts: 8521
You really need to read this < grin > : http://www.javaranch.com/campfire/StoryInner.jsp


"JavaRanch, where the deer and the Certified play" - David O'Meara
Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
Thanks Nathan. So, all inner classes (other than top-level static nested inner classes) cannot have static members other than primitive or String constants. Is that correct?
Cindy...the link above was very entertaining.. Thanks.
Why can't non-static inner classes have static members? Top-level classes have 'em.
Qoute from the campfire story:
"Another warning about all inner classes is that they can't declare any static members unless they are compile-time constants and are primitives or Strings. (This does not apply to static nested classes, of course). But don't worry -- the compiler will stop you if you try."
Thanks in advance.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Yes, you are correct! Non-static inner classes cannot contain static methods, and cannot contain static attributes, unless they are declared final, and are primitives or Strings. ( I think that Sun threw that last part in there just to be confusing... )
I think that non-static inner classes cannot have static members because they require an instance of the outer class to exist... static members can exist without any instances of a class. These would cancel each other out if non-static inner classes were allowed to have static members...

non-compiling example:


Okay... in this example, you should be able to call the getNum() method of topLevel, or access the outerInt without first constructing a new instance of the topLevel class... you would just call them directly since they are static. However, you cannot do the same to the innerInt or getInnerNum, because the innerClass class requires an instance of topLevel to be created before it exists... you can't say topLevel.innerClass.getInnerNum() because innerClass is not static... you would have to say new topLevel().innerClass.getInnerNum() or something ( if static members of non-static inner classes were allowed... ), and this is bad because you are having to reference something that is static through a referece... something that shouldn't happen...

Sorry for being so confusing...
-Nate
Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
Ohhh..okay..I get it now...yeah, I agree with you that referencing static members of non-static inner classes through an
instance of the outer class defeats the purpose of having the static members in the first place. Inner classes are a lot clearer now. Thank you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: inner class