• 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

inner classes

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Select all correct answers.
answer's given are 1,2 and 3.Please tell me whether they are correct or not.Is this question not poorly designed?
If u feel 2 option is coorect, please provide an explaination for that.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)All static variables
2)All final variables
3)All instance variables
4)Only final instance variables
5)Only final static variables

The answers are 1,2,3
2. Because inner class can access final automatic variables and global variables also. But they cant access the automatic variables if they are not final.
ex: public class TestClass
{
private int outervar;
private final int o1;
public void amethod(String s, final int v)
{
String str;
class inner
{
s="hello"; //illegal because it is not final
v=5; //legal cause it is final
str="bye" //illegal
o1++;//legal cause class level variable
outervar++;//legal
}
}
}
Thanks
Vishal Shah

------------------
-----
SCJP2
-----
 
reply
    Bookmark Topic Watch Topic
  • New Topic