Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from JQ+ mock exam

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep you are right about that.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3712
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

-Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus


[This message has been edited by Paul Anil (edited March 20, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic