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 overloading overriding etc 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 "overloading overriding etc" Watch "overloading overriding etc" New topic

overloading overriding etc

Karin Paola Illuminate
Ranch Hand

Joined: Oct 18, 2002
Posts: 109
What is the answer of the following question?
What will be the output of the following code?

1. Compilation succeeds, the output is: New value: 1
2. Compilation succeeds, the output is: New value: 0
3. Compilation fails, cannot create a class within the method body foo() at line 4
4. Compilation fails, cannot access the member variable ii from the class Inner at line 6
5. Compilation fails, the constructor from the test class should have been declared as public at line 4

Answer 4 is correct, the member variable should have been declared as final. It is impossible to change the variable ii within the test class.
The class test is called a local class and as such can only access the list of parameters defined in the method or access final fields from the top level class.
You have answered: [ 2 ]
I read somewhere "Local inner classes can access all the variables and methods of the enclosing class, but can only access final local variables and method arguments of the enclosing method." And, therefore answered "2". But, that seems not to be the correct answer.
(BTW. I think the ' in the code is a typo, and I did not pay attention to it.)
[ April 12, 2003: Message edited by: Karin Paola Illuminate ]

I not only use all the brains that I have, but all that I can borrow. [Laurence J. Peter]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You are correct, Karin, the code compiles (after changing the single quotes) and gives the result 0.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Karin Paola Illuminate
Ranch Hand

Joined: Oct 18, 2002
Posts: 109
Thanks for replying.
(I contacted the site owners, and the answer will be changed.)
I agree. Here's the link:
subject: overloading overriding etc
jQuery in Action, 3rd edition