File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help regarding Inner classes.. 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 "Help regarding Inner classes.." Watch "Help regarding Inner classes.." New topic

Help regarding Inner classes..

Sandip Sarkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 55
Hi All,

As per Kathy sierra "a local class declared in a static method has access to only
static members of the enclosing class, since there is no associated instance of
the enclosing class. If you're in a static method there is no �this�, so an inner
class in a static method is subject to the same restrictions as the static
method. In other words, no access to �instance variables�."

But in the code below i can access the non-static variables also of the outer class.Kindly explain.

Sergey Petunin
Ranch Hand

Joined: Dec 16, 2007
Posts: 44
In your code, you create an instance of the code9 class.

Generally speaking, using an instance, you can access any of it's fields which are visible in your code (in your case of an inner class - that would be any fields at all, even private).

What Kathy Sierra means is, you cannot access the fields without creating an instance, because there's no instance of the code9 class connected to the inner class directly.
Panseer Kaur
Ranch Hand

Joined: Nov 01, 2007
Posts: 44
Yes you can access it because you created an instance of that object in your method. But without creating an instance of it in your method you wouldn't be able to. When it says you cannot it means you cannot do it without creating an instance. Basically it means "Don't think you can create a static method then call an instance variable, because you can't". You can however pass an object as a argument to one of those methods or you could just create it yourself if you needed access.

Here is some example code of what works and why.

class Child
int x = 5; //Here is a instance variable we will try to get
static int y = 10; //A static variable that will be accessible to everyone

public static void main (String args[]) {

static void testMeMethod(){
class test {
void methodTest(){
Child a = new Child();
System.out.println(a.x);// This works because you created an instance
System.out.println(Child.y);// This works because the variable is static
System.out.println(this.x); // This does not work because this method is static
// and won't have access to the "this object"

test t = new test();

[ December 17, 2007: Message edited by: Panseer Kaur ]
Sandip Sarkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 55
Thanks a lot to you all.
i missed the point that the text talks of the point where in we are about to access non-static fields without an instance.

Thanks again for all your efforts.
I agree. Here's the link:
subject: Help regarding Inner classes..
It's not a secret anymore!