This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static  method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static  method" Watch "Static  method" New topic
Author

Static method

Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
Hello Friends
Please clear the following problem ----------------
class Test
{
static void show()
{
System.out.println("Show method in Test class");
}
}
public class Q2 extends Test
{
static void show()
{
System.out.println("Show method in Q2 class");
}
public static void main(String[] args)
{
Test t = new Test();
t.show();
Q2 q = new Q2();
q.show();

t = q;
t.show();
/* q = t; //COMMENTED
q.show();*/

}
}

Since static method cannot be overridden , so above program must give compiler error but its printing ----------
Show method in Test class
Show method in Q2 class
Show method in Test class
I am with that, kindly help me.
Regards,
Manish Singhal
Harry Singh
Ranch Hand

Joined: Sep 22, 2000
Posts: 53
Manish,
You can override static methods in a sub class, only if the overriding method in the subclass is also static. In your example if you remove the static keyword from the method show in the subclass Q2 you will get the error. The text of the error will be self explanatory.
Thanks
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104

Thanks Harry for that reply.
As you said that we can override a static method in subclass if the overriding method in subclass is also static, if it is so then since methods are resolved at runtime then
After executing
t = q;
t.show();
the output should be
Show method in Q2 class
but the method of superclass is executed.

Please say soemthing abt this.
Manish
Kishan Kumar
Ranch Hand

Joined: Sep 26, 2000
Posts: 130
Hi,
Static methods are not OVERRIDDEN but are HIDDEN. You should
know this difference. Static methods does not come under runtime Polymorphism. They are known at compile time itself.
The CLASS owns the STATIC method and not the object of the class.
So only the class type determines which static method is invoked
and not the object at runtime which is determining the static method.
THERE IS NO WAY A REFERENCE OF SUPERCLASS CAN CALL THE STATIC
METHOD OF THE SUBCLASS.

Both will print This is superclass.
Now you can see calling a static method does not even need a
object, a null is suffice, it gives no compilation error.
The only thing a compiler needs is you have to have a reference
varibale of a class that has the static method no matter what
object it contains at compile time or runtime, even null would do.
Hope this helps.


------------------
Regards,
V. Kishan Kumar


Regards,<BR>V. Kishan Kumar
Trupti Mehta
Ranch Hand

Joined: Oct 08, 2000
Posts: 79

Hi Manish,
I had tried the a similar program while preparing for my JCP exam. After that I had concluded the following.
Static methods can be overridden ONLY by static method.
When it is overriden, then parent's method will be called when you create a reference variable of parent pointing to a child object. In such a case, the child's method is not being called at all.
Although this seems to be contrary to what has been the rule "for a parent's reference variable pointing to a child, the methods will be of the child and variables will be of parent", I conclude that java team may have kept a separate rule for static method.
Trupti Mehta

Regards,
Trupti (SCJP)
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
Thanks a loooott Kishan and Trupti.
Both of the explanations are excellent , and have helped me to clear my doubt.
One more thing I would like to ask is that as Trupti said ----

Although this seems to be contrary to what has been the rule "for a parent's reference variable pointing to a child, the methods will be of the child and variables will be of parent", I conclude that java team may have kept a separate rule for static method

So do I have to cram this rule that.......
Static methods can be overridden ONLY by static method.
When it is overriden, then parent's method will be called when you create a reference variable of parent pointing to a child object. In such a case, the child's method is not being called at all.

...........or there is some logic behind it.
I Hope to get a reply,
Thanks once again
Manish Singhal
Sagar Sharma
Ranch Hand

Joined: Aug 31, 2000
Posts: 92
hi,
i think you r too confused....
"Static methods cannot be overridden to be non static" is the rule.

HOPE THIS HELPS!!!
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
Sagar, I think u misunderstand my question. The last message I post is to ask that,
When a static method is overridden by static method......then parent's method will be called( in the child class) when you create a reference variable of parent( in the child class) pointing to a child object. In such a case, the child's method is NOT being called at all. [ as it would have been called if the both methods (in parent class as well as in child class) ..... would be non-static] .......... so,
I am asking that is it a rule ??? I hope Trupti Mehta will get my question and will reply to it.
Manish

[This message has been edited by Manish Singhal (edited October 09, 2000).]
mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
Manish I think Kishan has given you a precise reply. Static methods are not overriden, but they are hidden. Your code above is a good example of showing that.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
I think it may be more clear if you use the class names to call the static (class) methods instead of using the object.

Does this help you?
Marilyn

[This message has been edited by Marilyn deQueiroz (edited October 09, 2000).]


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static method