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 Confused with static methods 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 "Confused with static methods" Watch "Confused with static methods" New topic
Author

Confused with static methods

revup triv
Greenhorn

Joined: Aug 17, 2003
Posts: 27
Have a look at follo. code,
class SupTest{
static void a() { System.out.println("From SupTest"); }
//void printa() { a(); } //comment1
}
class SubTest extends SupTest {
static void a() { System.out.println("From SubTest"); }
void printa() { a(); } // comment2
public static void main(String args[]){
new SubTest.printa();
}
}
If I apply comment on comment1 line & complile & run, it prints "From SubTest". But if I comment the comment2 line & remove comment from comment1 line it prints "From SupTest". What is the rule behind overriding static method? When which one gets called? Please help me understand it.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
the code you posted does not compile . Let's use the "code" tags and fix it

static methods are not overriden.


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Are you sure this compiled and ran? I see two problems without even going into the static part:
1. You have a typo in line 10 ("Subtest" instead of "SubTest").
2. "new SubTest.printa()" is trying to invoke the constructor for a static inner class (printa inside SubTest), which you don't have.
I just don't see how this code could run. Maybe if you copied and pasted the code from your text editor I could re-evaluate it or someone else could help.
But to answer your question: Static methods cannot be overridden. When you declare a method with the same signature and return type in a subclass, you're actually redefining the method instead of overriding it.
Regards,
Jeff


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
But to answer your question: Static methods cannot be overridden. When you declare a method with the same signature and return type in a subclass, you're actually redefining the method instead of overriding it.
yup, we were both trying to correct the code at the same time
Here's another example revup:

This one is using static methods. It prints : supper class
Now, if we take the static away:

it prints subclass.
hope this helps
Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
I ran this code , and i am also getting the same answer. i.e first time
"From SubTest" and 2nd time "From SupTest" without any compile error.
I think static methods cann't be override to non-static but we can override them to static.Second time we r commenting comment 2 line it will call printa method from superclass. Now method a() is static so it is class specific so not the part of object. hence methodcall a() will results into
method invocation in superclass.
S. Sumukh
Ranch Hand

Joined: Aug 27, 2003
Posts: 31
I modified the given code..see the commented line..
and the output was "subclass"..
that means static method is overridden...!!
plz correct me if I m wrong.
TIA
sumukh

class SupTest{
static void a() {
System.out.println("supper class");
}
}
public class SubTest extends SupTest {
static void a() {
System.out.println("subclass");
}
public static void main(String args[]){
//Instead of SupTest i used SubTest on LHS
SubTest sub = new SubTest();
sub.a();
}
}
Uma Balu
Ranch Hand

Joined: Aug 22, 2003
Posts: 61
Hi Sumukh,
In your code, you have created an instance of the derived class and invoked the method a(), there is no runtime binding happening.
SubTest s = new SubTest() ;
s.a() ;
It will obviously invoke only the sub class method, since s is an instance of SubTest, it is as good as saying SubTest.a()
If you try creating an instance of super class and instantiate with the derived class , ie if you say,
SupTest s = new SubTest() ;
s.a() ;
This will invoke SupTest class's method, since a() is static and it can't be overridden. Try making a() as non-static method and try the above code, it will print "super class".
S. Sumukh
Ranch Hand

Joined: Aug 27, 2003
Posts: 31

SupTest s = new SubTest() ;
s.a() ;
This will invoke SupTest class's method, since a() is static and it can't be overridden. Try making a() as non-static method and try the above code, it will print "super class".

Hi uma
If u make a() as nostatic it will invoke subclass' a() not of the superclass..and it will print "sub class"
or was that a typo mistake u made ?
Uma Balu
Ranch Hand

Joined: Aug 22, 2003
Posts: 61
Hi,
Yep... was a typo mistake :roll:
Uma....
 
Don't get me started about those stupid light bulbs.
 
subject: Confused with static methods