What is the answer of the following question? [JavaCertification.com] 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
Feedback: 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]