• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Innner class inside a method

 
Porchelvi Vendhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic