Ok this is giving me trouble: Which of these statements are true? A) Non-static inner class cannot have static members. B) Objects of top level nested classes can be created without creating instances of their Outer classes. C) Member variables in any nested class cannot be declared final. D) Anonymous classes cannot have constructors. E) Anonymous classes cannot be static My answers were: A, B, D. JQPlus says the "correct" answers are: B, D, E. Had I read E a little more carefully, I probably would have thought A, B, D, and E were correct (although I could only pick three, I would have selected B, D, E). But I thought for sure A was correct. In my Khalid Mughal book, it says: "Non-static inner classes ONLY DEFINE NON-STATIC MEMBERS" Thus A is correct. However, the JQPlus "general comments" section says a non-static inner class CAN DEFINE final static fields (but not methods). I have NEVER read that ANYWHERE. Is this just a little "bit" of info that hasn't surfaced much yet? I hope my question makes sense!
Tanveer Mehmood
Ranch Hand
Joined: Feb 23, 2001
Posts: 51
posted
0
A) Non-static inner class cannot have static members. B) Objects of top level nested classes can be created without creating instances of their Outer classes. C) Member variables in any nested class cannot be declared final. D) Anonymous classes cannot have constructors. E) Anonymous classes cannot be static (A) is correct as inner classes can never have any static members. Following code would flag a compile time error at line one. class outer { class inner { // static int temp; (1) } } (B) is straight away wrong. (C) is again wrong as there is no such restriction. (D) is true as anonymous classes don't have any names and cannot declare constructors. (E) Would have been true if stated like this...
"Anonymous classes cannot be declared static" Being static or non-static depends upon the context in which they are declared. See table 7.1 at page 255 in your book. So only A and D are true in my opinion.
Tanveer Mehmood
Ranch Hand
Joined: Feb 23, 2001
Posts: 51
posted
0
This is with reference to my previous post as I misread the option B which is true. A) Non-static inner class cannot have static members. B) Objects of top level nested classes can be created without creating instances of their Outer classes. C) Member variables in any nested class cannot be declared final. D) Anonymous classes cannot have constructors. E) Anonymous classes cannot be static (A) is correct as inner classes can never have any static members. Following code would flag a compile time error at line one. class outer { class inner { // static int temp; (1) } } (B) is straight correct. (C) is again wrong as there is no such restriction. (D) is true as anonymous classes don't have any names and cannot declare constructors. (E) Would have been true if stated like this...
"Anonymous classes cannot be declared static" Being static or non-static depends upon the context in which they are declared. See table 7.1 at page 255 in your book. Yes! I do agree with you. Only A, B and D are true in my opinion.
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
See the stupid "general comments" said if a "field" is FINAL STATIC than it CAN be declared in a non-static inner class .. WHAT?? makes no sense!!
Tanveer Mehmood
Ranch Hand
Joined: Feb 23, 2001
Posts: 51
posted
0
FINAL STATIC!!! The only convention that I remember about STATIC FINAL is that fields declared in INTERFACE are implicitly STATIC FINAL in other words CONSTANTS....
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Yep you are right about that.
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2912
posted
0
The given answer is correct. The following code compiles without any problem... class outer { class inner { final static int temp = 10; } } As you can see inner is a non-static inner class and temp IS static. (though final) Makes sense???
As far as Anonymous classes are concerned, they can NEVER be static. Whether created in a static or non-static context. It DOESN't matter. You can even verify it using javap.