• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variable referencing  RSS feed

 
Vidya Shivram
Ranch Hand
Posts: 38
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was one of the quiz question.
which variables may be referenced correctly in the iMethod ..



Correct Answer was :
A, B, C and E are correct. Since Inner is not a static inner class, it has a reference to an enclosing object, and all the variables of that object are accessible. Therefore A and B are correct, even if b is private. Variables in the enclosing method are only accessible when they are marked as final hence c is accessible but not d. E is obviously correct as it is a parameter to the method containing line 12 itself.




where option A - a, B-b, C-c D-d, E -e

I have added line 1 to 5 to check and I get an error on line 3 but not on line 4  and 5. According to answer, d is not accessible from imethod then where am I going wrong ? please someone help me to understand the concept of this variable reference.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to answer, d is not accessible from imethod then where am I going wrong ?

I'm not sure.  What is happening that you don't understand?  You get a compile error trying to access the variable d just like the answer said you would.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The indent formatting of the code needs some work.  The code indentation after the main() method's ending } makes it look like the code is inside of the main method.
The lack of indentation for the Inner class hides that it is inside of method()
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!