• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner class static or no?

 
Juan Carlos Oidor Gonzalez
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
look this question:
Which of the following statements are true?
1 An inner class may be declared private
2 An inner class may be declared static
3 An inner class defined in a method should always be anonymous
4 An inner class defined in a method can access all the method local variables
5 Construction of an inner class may require an instance of the outer class

Answer and Exp : 1, 2, and 5 are correct. Inner classes may be defined with any accessibility, so private is entirely acceptable and 1 is correct. Similarly, the static modifier is permitted on an inner class, which causes it not to be associated with any particularinstance of the outer class. This means that 2 is also correct. Inner classes defined in methods may be anonymous--and indeed often are--but this is not required, so 3 is wrong.4 is wrong because it is not possible for an inner class defined in a method to access the local variables of the method,except for those variables that are marked as final. Constructing an instance of a static inner class does
not need an instance of the enclosing object, but all non-static inner classes do require such a reference, n that reference must be available to the new operation.The reference to the enclosing object is commonly implied as this, which is why it is commonly not explicit. These points make 5 true.
But J2SE specification says:
"An inner class is a nested class that is not explicitly or implicitly declared static. "
"Inner classes include local, anonymous and non-static member classes "
So 2 is INCORRECT or no?
I guess that the exam uses the terms: Top-level nested classes and interfaces to refer to static members inside a class. And an inner class like instance members of others class.So a inner class can never be declared static, but a nested class must be declared static.
am I wrong or An inner class may be declared static? TO THE EXAM WHAT IS CORRECT? explain pls.
 
Phil Rhodes
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you'll get a question on the real exam that
is based on that distinction (of terminology). If you should
happen to, I'd go with what the spec says...
FWIW, I don't remember any questions on my exam where that distinction would have made a difference.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic