• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

non-static inner classes with static members

 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while taking some mock exams I got 2 contradictory statements.
1) non-static inner classes cannot have static members.
2)inner classes may only declare static members if are compile time constants.
I tested this and found that statement 2 is correct. they can have static members if they are also final. I only tested it for member variables(not for methods).
 
Sam Zheng
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to JLS non-static inner class can have static
variables if they are compile-time constants.
From several test cases I ran, it seems that you cannot
have a static local variable, even in a static context
(static method, for example). If this a general
rule or just an exception...oh no, not the checked exception
you may think of :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic