• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wrong explanation in javacertificate exam

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have agreed with you, but surprisingly (to me!) that code compiles fine here with JDK1.5.
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic