• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Why calling super to reach a static member is ALWAYS illegal?

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just remember that the dot operator could be used to reach a static memeber...like Obj.staticMethod(). So why does this fail:

class HaveStatic {
static String shiny() { return "1"; }
}
class Test extends HaveStatic {
public static void main(String [] args) {
String s = shiny() + getShiny();
s = s + super.shiny();
System.out.println(s);
}
static String getShiny() { return shiny(); }
}
[ May 26, 2006: Message edited by: Firas Zureikat ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this and super
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you're not allowed to use super that way.
super refers to the superclass instance, which you don't have inside a static method.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since 'this' refers to an instance of the current type, static methods doesn't own any particular instance, it doesn't own a 'this' pointer. 'super' point to the super-class portion of the current instance, if static cannot even own a copy of the current instance how can it own the super-portion of it? Simply if you don't own a pie (this) you definitely don't have access to a portion of it (super).
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thanks...So no "this" nor "super" could be used in any static methods.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't have access to a this or super in a static method. But when you do have access to this or super, i.e. in an instance method, the trick of invoking a static method a.k.a. class method using an object reference works just fine:



The type of the reference is used when invoking static methods using object references. The type of this is always the class in which it is used (Bla) and the type of super is always the superclass of the class in which it is used (SuperBla).
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I summarize Dick's post we can say that
We can use super and this in a non-static method to access static methods BUT we can not use super or this in a static methods

Am I right?
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right.....
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic