my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please explain this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please explain this" Watch "please explain this" New topic
Author

please explain this

payal sharma
Ranch Hand

Joined: Oct 04, 2000
Posts: 57
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Apr 02, 2001
Posts: 1157
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Apr 11, 2001
Posts: 356
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

Joined: Jul 15, 2001
Posts: 112
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

Joined: Apr 02, 2001
Posts: 1157
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: please explain this