This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes Accessing static method with super Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Accessing static method with super" Watch "Accessing static method with super" New topic
Author

Accessing static method with super

MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
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?


I am slow but sure
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
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

Joined: Sep 24, 2009
Posts: 188
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

Joined: Jan 26, 2007
Posts: 160
"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

Joined: Sep 24, 2009
Posts: 188
But Manish, I had read in book that super keyword cant access static members of the super class
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

Joined: Sep 26, 2009
Posts: 188
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

Joined: Sep 24, 2009
Posts: 188
Rahul
Your last comment was pretty straight forward and thanks to all those who cleared my doubts.

---Mahesh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Well done, Rahul.P
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Thanks for appreciation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing static method with super