• 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

Innner class inside a method

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I have a doubt.Can methods inside Inner classes ,defined inside a method, access any member variables of the enclosing class?For example:
public class MyOuter{
private int s;
public void get()
{
class MyInner(
public void r()
{
//Can method r access instance variables of MyOuter ,say s?
}
}
}
}
Thanks ,
Porchelvi Vendhan
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello porchelvi,
Local class methods can access both static and non-static members of the enclosing class, PROVIDED the local class is
non-static. If it is static then only static members can be accessed
Hope it helps
Ragu
 
Porchelvi Vendhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ragu!
Thanks for your reply.I think local classes can't be declared as static.
Porchelvi Vendhan
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Porchelvi Vendhan:
Hi Ragu!
Thanks for your reply.I think local classes can't be declared as static.
Porchelvi Vendhan


Yes. They cant be declared static.
However, the context (ie the method may be static)
Even so, still non-static members of the outer class can be accessed
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think a static class is able to access non-static fields of the enclosing class. The reason is the same as a static method is not able to access instance members.
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ragu Sivaraman:

Originally posted by Porchelvi Vendhan:
[b]Hi Ragu!
Thanks for your reply.I think local classes can't be declared as static.
Porchelvi Vendhan


Yes. They cant be declared static.
However, the context (ie the method may be static)
Even so, still non-static members of the outer class can be accessed
[/B]


I am sorry for the typo.
My intention was that
"Even so, still non-static members of the outer class can't
be accessed"
Some times mind/fingers go outof sync
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Botella:
I don't think a static class is able to access non-static fields of the enclosing class. The reason is the same as a static method is not able to access instance members.


Static method can access instance members (non-static)
via an object reference
 
Tell me how it all turns out. Here is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic