hi in one question of javacertificate ...
Correct, you chose answer 1.
1. public class Inner {
2. private int ii = 0;
3. public void foo() {
4. class test{
5.
test(){
6. ii++;
7. }
8. }
9. Object obj = new test();
10. }
11. public static void main (
String args[]){
12. Inner in = new Inner();
13. in.foo();
14. System.out.println ("New value: " + in.ii);
15. }
16. }Compilation succeeds, the output is: New value: 1
Compilation succeeds, the output is: New value: 0
Compilation fails, cannot create a class within the method body foo() at line 4
Compilation fails, cannot access the member variable ii from the class Inner at line 6
Compilation fails, the constructor from the test class should have been declared as public at line 4
Answer 1 is correct.
ii from the top level class is initialized with 0 when the class is constructed.
Line 13 executes the instance method of the class Inner, inside this method the local class test is defined at line 4 and instantiated at line 9. The constructor of the test class increments the value of ii. The output is 1.
At line 14 the value of ii is printed out, the class Inner has access to the variable, even if the variable has the private access modifier.
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 enclosing method and can access non-final fields from the top-level class. ----------------------
here its above written that it can access the parameters difined in method
i think its wrong according to K&B ..as it can't access the local memembers of that method in which it is defined.. ?
i m right ?