• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Inner Class

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi J'pros,
i'd dbt in Inner class, i was taking one mock exam, the question abt inner class abt the scope of variables..which are the variables can be placed in place XX, my answer is ID and nn but to my surprise name is also in the answer list.. when i went thru RHE it says if a inner class is defined inside a method of a outer class only variables accessible is if it's marked as FINAL then how name is accessible.
Any explanation is highly appreciated.
public class Outer{
private static final int ID = 5;
private String name ;
public voic methodA(final int nn){
int serialIN = 22;
class Inner{
void showresult(){ //Here the parantesis is other way like this way }first of it's printing mistake..
System.out.println("Rslt="+XX);
}
}
new.inner.showresult();
}
}
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
Inner class, unless 'static',have access to the variables of the enclosing class instance. ADDITIONALLY, inner classes defined in method scope have access to 'final' variables of the enclosing method.
Hope u got the point.
kodali
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, in case of inner classes defined in methods..
Variables declared as final in enclosing method, all other
variables of the top level class can be accessed.
- cheers
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A local inner class has free access to all the member variables of the outer class AND the final variables defined in the method within which the inner class is defined
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic