Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JQPlus Question on Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JQPlus Question on Inner Classes" Watch "JQPlus Question on Inner Classes" New topic
Author

JQPlus Question on Inner Classes

Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
Question ID :953582884020
Which of these statements are true? Select 3
a) Non-static inner classes cannot have static members
b) Objects of top level nested classes can be created without an instance of the outer class
c) Member variables in any nested class cannot be declared final
d) Anonymous classes cannot have constructors
e) Anonymous classes cannot be static
My answer - b) & d)
I don't understand why JQPlus also choses e) as the right answer.
I have read inner classes from Khalid Mughal & its very clearly given that we can have Anonymous Inner classes that are static. He has even given examples demonstrating the same... Can someone help, please !!!


Himanshu Jhamb<br />SCJP2 v1.2 (April 2002)<br />SCJP2 v1.4 (May 2002)
Rajeev Nair
Ranch Hand

Joined: Mar 11, 2002
Posts: 51
I think option a) is true right?. Non static inner class cannot have static members unless they are compile time constants like public static final.


Raj<br />Sun Certified Java Programmer
Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
Well !
If its a compile time constant...
i.e. static final int a = 0;
... then it is static, also... right?
So, option a) is false.
- Himanshu
Tina Ang
Ranch Hand

Joined: Mar 12, 2002
Posts: 58
I agree with Himanshu that a) is false.
I think answer e) is correct. According to JLS 15.9.5 "An anonymous class is never abstract. An anonymous class is always an inner class; it is never static. An anonymous class is always implicitly final."

Tina
JLS 15.9.5
Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
Well!
In that case, can we get Mr. Khalid Mughal to comment on this? Val, anyway, we can manage that ?
I think I would stick with what JLS says.
Waiting for your comments, Val.
thanks
- Himanshu
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
The best way to get the correct answers is to write a little piece of code for each option and see how it behaves.
a. FALSE

Compiles fine.
b. TRUE

compiles fine
c. FALSE

compiles fine
d. TRUE

Does not compile. The error is:

Which means that the compiler treats Runnable() as a method and not a constructor.
e. This last one is tricky and the JLS and KM seems to be of different opinions. JLS says that anonymous classes cannot be declared static and KM says that an anonymous classes declared within a static context is implicitely static.
Well, it is true that an anonymous class cannot be declared static. Moreover, an anonymous class is a local class, it is always created within the context of another method or initializer and there is no point in declaring a local method static even if the context is static. An anonymous class just serves the purpose of declaring and creating a instance of a class "on the fly". The result is always an instance of a class that can usually be returned. That object can be used anywhere in the program. It can be assigned to a static member or an instance member (see code below). Moreover an object is never static, it is the reference that points to it that can be static but not the object itself.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
Thanks Val ... for all the trouble you take to explain stuff
Indeed, very helpful.
- Himanshu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JQPlus Question on Inner Classes