• 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

static method local classes

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Why they say that the special form of this construct cannot be used in a static local class since it does not have any notion of an outer object. The static local class cannot refer to such hidden declarations. Can you please help me guys?? Conside the code below,



Thanks in advance.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi dear,

class Base{
protected int nsf1;
}
class TLCWithLocalClass extends Base{
private int nsf1;
void nonStaticMethod(final int p){
int f3 = p;
class NonStaticLocal extends Base{
//double ff = nsf1; //This is ok
double ff = TLCWithLocalClass.this.nsf1; //This is ok because //class is defined within a method that is non static so it's //an Inner class and has an implicit reference of Outer //class(TLCWithLocalClass) so it can use it's variable even //directly.
}
}
static void staticMethod(){
class StaticLocalClass extends Base{
double gg = TLCWithLocalClass.this.nsf1; //This is not ok //because class is defined within a method that is static SO //IT'S NOT AN INNER CLASS BUT A STATIC NESTED CLASS which has no //notion of outer class or "this" hence you get an error massage //saying "non-static variable this cannot be referenced from a //static context".
}
}
}
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,


raunak saxena posted November 14, 2006 09:27 AM
hi dear,
(...)
hence you get an error massage (...)



Yeah, an error massage, that's what we need now!

To Jothi:


say it with Graemlins:


Yours,
Bu.
 
Maneessh saxena
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Burkhard Hassel:
Hi ranchers,


raunak saxena posted November 14, 2006 09:27 AM
hi dear,
(...)
hence you get an error massage (...)



Yeah, an error massage, that's what we need now!

To Jothi:

Yours,
Bu.



class Base{
protected int nsf1;
}
class TLCWithLocalClass extends Base{
private int nsf1;
void nonStaticMethod(final int p){
int f3 = p;
class NonStaticLocal extends Base{
//double ff = nsf1; //This is ok
double ff = TLCWithLocalClass.this.nsf1; //This is ok...(1)
}
}
static void staticMethod(){
class StaticLocalClass extends Base{
double gg = TLCWithLocalClass.this.nsf1; //This is not ok...WHY??(2)
}

}

}
(1) This is ok because class is defined within a method that is non static so it's an Inner class and has an implicit reference of Outer class(TLCWithLocalClass) so it can use it's variable even directly.

(2) This is not ok because class is defined within a method that is static which causes this class to be static implicitely which has no notion of outer class or "this" hence you get an error massage saying "non-static variable this cannot be referenced from a static context".


Mr. Bu.....

you guys never try to understands the logical thing instead tries to find bug always.

i was perfectly correct but the it was the printing mistake now see the code and let me know whether you understand or not.


regards
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raunak,

there was nothing wrong with your explanation - I only found your typo funny.




Yours,
Bu.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic