Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Super Inside the Static

 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FROM web page


Whether super can be used inside the static methos?

Anserr given in the site:
[/CODE]
The keyword, this, refers to the instance on which the method has been invoked. A static method--also known as a class method-- is not invoked on a particular instance of an object, but is instead invoked on the class. Since a static method is not associated with a particular instance, an attempt to use the keyword, this, within the body of a static method results in a Compile-time error. Similarly, the keyword, super, can not be used within the body of a static method.
[/CODE]
 
Arpit Khandelwal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you can not use super in a static context.

just to prove this point , try the following code ::

class base {
static void f1(){
System.out.print("base");
}
}
class test extends base {
static void f2(){
------>super.f1();
System.out.print("base");
}
public static void main(String args[]) {
test.f2();
}
}

the compiler would complain "non-static variable super can not be referenced from a static context"
 
Nomula vasudevu
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods cann't be overriden. Then how can we use super keyword. Therefore the option static methods may use super keyword is not true. Am I correct ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic