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

Inner class code,confusing

 
WiLL Tao
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code about static inner class access private
variable.please tell me reason in my comment,Thanks
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't really a question about inner classes. Also, please note this is NOT an inner class anyway, it's a nested top-level class. Static classes can't be inner classes.
But your problem is that you're trying to include a statement in a location where it isn't allowed. A nested class obeys the same rules as a top level class. You can't just throw a println() statement anywhere you want, it has to be inside a method or a constructor.
For example, you can't do this :
class Foo{
System.out.println(); //compiler error
}
The only things allowed outside of a method or a constructor are variable declarations with or without an initializer, and static or instance inizitializers, as you have used in your code.
To fix the above class, you either delete the println() or put it in a method:
 
WiLL Tao
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob
You correct me,now i am clear.
[ March 31, 2002: Message edited by: WiLL Tao ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic