Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner class

 
Gaia Nathan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it?
 
Gaia Nathan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only static inner classes can have static methods... for all others it is not allowed...
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really need to read this < grin > : http://www.javaranch.com/campfire/StoryInner.jsp
 
Gaia Nathan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic