File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused with static methods

 
revup triv
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code you posted does not compile . Let's use the "code" tags and fix it

static methods are not overriden.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yep... was a typo mistake :roll:
Uma....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic