Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing static method with super

 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Super1
{
static void test(){System.out.println("testSuper");}
};

class testSuper extends Super1
{
void test1(){super.test();}
public static void main(String[]s){testSuper t = new testSuper();t.test1();}
};

In above code sample we are actually accessing static method test. How does it work as we cant access static methods with super keyword?
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting observation! If test1() would have been static too, you could not done that. Mind it, I am theorizing.

To call a static test1(), you do not need to create an object of testSuper class, in that case, if static test() was called from inside of test1(), it was like access super static method test() without declaring its type or defining its object.

I mean : testSuper t; on another line, t.test(); Think about it.

Even though a subclass has access to all methods in its super class, but this holds true only if you have created its object.
Now come to your question, your test1() is instance method, so you needed object of testSuper(). This lets you access super static or non-static methods.

PS- someone correct me, if I am wrong.
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul

As per your comment, if I declare test1 method to be static then definitely compiler wont allow to use not static super keyword inside static context. According to your second suggession, if I just declare reference type of testSuper but dont create an instance of it like testSuper t; t.test1(); where test1() is instance method then I get an error that variable t might not have been initialized as the declaration is inside method.

So the basic question is how am I able to access static superclass method using super keyword?
 
Manish Singh
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"super" is the reference to the super class . and it can access the static members of the super class the same way as this can be used to access the static members of the current class.

In general we should not use the instance references to access the static content as it is confusing
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Manish, I had read in book that super keyword cant access static members of the super class
 
Campbell Ritchie
Sheriff
Pie
Posts: 50246
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether it can be done or not, it sounds like confusing technique. Why do you want access to a superclass static member? Why have you hidden it in the subclass?
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Porbably Manish is trying to understand. So, to him, to know what can be done and what can not be done is more important than a wrong or right technique. In my earlier post, I clarified that to access a static member, you need declaration of that type So, Super1 s; now you can say s.test(); Here s is not static. The same thing you are achieving through super. So, you are accessing static method via a non-static reference. Manish, either you wrongly interpreted my earlier post, or I have not clearly put my point straightforward.
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul
Your last comment was pretty straight forward and thanks to all those who cleared my doubts.

---Mahesh
 
Campbell Ritchie
Sheriff
Pie
Posts: 50246
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done, Rahul.P
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for appreciation
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic