• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

please explain this

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic