Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help regarding Inner classes..

 
Sandip Sarkar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Thanks.
 
Sergey Petunin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[]) {
Child.testMeMethod();
}

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();
t.methodTest();
}
}

[ December 17, 2007: Message edited by: Panseer Kaur ]
 
Sandip Sarkar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic