开发者

Static Methods in Java

I am new to Java Programming. Can anyone please explain me why the program outputs - "fa la" even though the static method is overridden. I read that static methods can't be overridden in Java? Please correct me if I am wrong.

public class Tenor extends Singer {  
   public static String sing() {   
      return "fa";   
   }  
 开发者_StackOverflow  public static void main(String[] args) {  
      Tenor t = new Tenor();  
      Singer s = new Tenor();  
      System.out.println(t.sing() + " " + s.sing());  
  }  
 }  
class Singer {   
   public static String sing() {   
       return "la";   
    }   
}  


You cannot override static methods in Java. It's simply calling the static methods directly on each class. BTW: As others have noted, it's considered bad practice to call static methods on instances. For clarity, you should do the following:

System.out.println(Tenor.sing() + " " + Singer.sing());

which is equivalent to the code you have written in your question.


Static methods should be accessed with the class name and not an object reference. The correct equivalent of what you wrote would be:

System.out.println(Tenor.sing() + " " + Singer.sing())

Java is guessing inferring which method you meant to invoke based on the type of object variable.

EDIT: As Stephen pointed out, it isn't guessing. Inferring would probably be a more accurate word. I was just trying to emphasize that calling static functions on object references might result in behavior you wouldn't expect. In fact, I just tried a few things and found out that my previous statement was incorrect: Java decides which method to call based on the variable type. This is obvious now that I think about it more but I can see how it could lead to confusion if you did something like:

Singer s = new Tenor();
System.out.println(s.sing());


t is of type Tenor and s is of type Singer.

When you invoke a static method on an instance (which is bad practice) you are invoking the static method on the class of the declared type.

i.e. t.sing() is equivalent to Tenor.sing()


Static methods are not overridden. They belong to the class in which they are defined. Calling a static method on an instance of that method does work the same way as calling it on the class, but makes things less clear and leads to people thinking they are overridden like instance methods.


Static methods can't be overridden.

the following resource will help you to understand better what is going on when you are trying override static method:

http://www.javabeat.net/qna/49-can-we-override-static-methods-what-is-metho/

Regards, Cyril


The code you wrote works fine, but probably not how you intended it to. Static methods can't be overridden in the traditional sense (or with the behavior you might expect). In terms of which version of the method is invoked, this is done at compile time, which explains the output you see.

Yes, and you should probably only invoke the methods using the Class name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜