This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Mock Exam Errata and the fly likes wrong explanation in javacertificate exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "wrong explanation in javacertificate exam" Watch "wrong explanation in javacertificate exam" New topic
Author

wrong explanation in javacertificate exam

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
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 ?


Thanks and Regards, Amit Taneja
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
I would have agreed with you, but surprisingly (to me!) that code compiles fine here with JDK1.5.
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
Originally posted by amit taneja:

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 ?


I think the author means that the parameters passed (which are defined in the signature) to the method.
Megha Shah
Greenhorn

Joined: Jul 13, 2005
Posts: 1
Class test is an inner class and Inner classes can accept all the member variables of the outer class. Thats the whole funda of Inner classes. Therefore class test can access the member variable ii of class Inner even though it has private access.
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
Well, here the dispute is not about accessing ii, rather the line which says the local class ( test is called a local class and not inner class as it appears inside a method ) can access the parameters defined in the method. This is true only if the local variable or a formal parameter is marked final.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wrong explanation in javacertificate exam