• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explain this

 
payal sharma
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class tq{
protected double x;
protected static int n;}

class ts extends tq{}
public class Test extends ts
{
private String str;

Test()
{
System.out.println("hello");
str="Private variable";
}
public static void testNest()
{
class nested{
void display()
{
// System.out.println(str);
System.out.println(x);
System.out.println(n);
}
};
new nested().display();
}
public static void main(String[] a)
{
Test t=new Test();
t.testNest();
}

}

Here I am getting erroe can't make static reference to non static variable double x in void display()
But in Khalid Mughal on page 239 an example is given and on line 19 they have access non static variable from super class in to static local call.
Please tell me where I am making mistake?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This compiles and runs:

Could some one explain further?
(modifier static was removed from testNest method)
------------------
Antti Barck
It Solutions Consultant, NSD Oy
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Payal,
If you examine your code, the nested class is a static local class.Hence you can not access the instance variables in this class.
However, you may inherit the non-static variables in the static local class.
So the following code would compile without errors :

Note that in the above code, the nested static class extends the class tq where the instance variable is defined.
May be this is what would have been implied in the Khalid's book.
Hope this helps,
Sandeep
SCJP2,OCSD(Oracle JDeveloper),OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited July 25, 2001).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Desai Sandeep:
clipclipclip
Note that in the above code, the nested static class extends the class tq where the instance variable is defined.
May be this is what would have been implied in the Khalid's book.

That clears it - I did not read the question...

------------------
Antti Barck
It Solutions Consultant, NSD Oy
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In Payal's example nested class is created inside a static method, so you have to treat nested in a static context.
Hope this helps.
Vanitha.
 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I thought Anonymous innner class and local class(inner class inside a mothod) cannot access the enclosing classes members unless they are FINAL.
Can somebody please explain this.
Thanx
Rajani
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajani,
The local class and the anonymous class can access all the variables of the enclosing class.They don't need to be defined as FINAL.
Only those varaibles which are defined in the local scope of the method needs to declared as FINAL for access in the local and anonymous classes.This makes sense, since we would want the local variables to exist even after the program(or enclosing class) has finished with its execution.
Hope this helps,
Sandeep
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic