Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner Classes..!

 
Nilesh Raje
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,

This question is from 4tests.com..!Please Justify the answers for this question.


Which variables can an inner class access from the class which encapsulates it?

1)All static variables
2)All final variables
3)All instance variables
4)Only final instance variables
5)Only final static variables

The Answers given are (1)(2)(3)

Thanks in advance..!
 
Don Retag
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
options 4) and 5) are mutually exclusive with options 1) 2) 3). If either one is true, the other is false. For class-level inner-classes, not method local inner classes, the class has full access to final and instance variables.
 
Santana Iyer
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inner class in above question means regular inner class defined inside class.

It can access all variable of enclosing class as it exists only with an enclosing instance.

For inner class defined inside method can not access local variables defined inside method however it can access final local variable. It can access encolsing instance members.

Hope this helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic