File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

non-static inner classes with static members

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

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

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 :-)
I agree. Here's the link:
subject: non-static inner classes with static members
It's not a secret anymore!