wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes non-static inner classes with static members Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "non-static inner classes with static members" Watch "non-static inner classes with static members" New topic
Author

non-static inner classes with static members

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

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


SCJP
Visit my download page
Sam Zheng
Ranch Hand

Joined: Nov 29, 2000
Posts: 61
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 :-)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: non-static inner classes with static members
 
Similar Threads
Inner classes; when should they be static vs. instance?
inner classes
Question regarind Inner Classes
JQ+ Inner Class Question
Non-static Inner Class