GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Innner class inside a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Innner class inside a method" Watch "Innner class inside a method" New topic
Author

Innner class inside a method

Porchelvi Vendhan
Greenhorn

Joined: Sep 08, 2001
Posts: 21
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

Joined: Jul 20, 2001
Posts: 464
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

Joined: Sep 08, 2001
Posts: 21
Hi Ragu!
Thanks for your reply.I think local classes can't be declared as static.
Porchelvi Vendhan
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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

Joined: Jul 03, 2001
Posts: 2120
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.

SCJP2. Please Indent your code using UBB Code
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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

Joined: Jul 20, 2001
Posts: 464
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Innner class inside a method