File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes static method overriding in same class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static method overriding in same class" Watch "static method overriding in same class" New topic
Author

static method overriding in same class

Pratap gogireddy
Greenhorn

Joined: Dec 03, 2011
Posts: 7

hi friends

I got this question in one of the MNC written exam

class abctest
{
static void m1(Object o){System.out.println("Hello Object!");}
static void m1(String s){System.out.println("Hello String!");}
public static void main(String[] args)
{
m1(null);
}
}

when i test on the System the OUTPUT is Hello String

Why it takes String Method instead of Object Method can any one explain for me please


Thanks in advance....
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

As far as I know, Java takes the most specific method that it can find.

null is valid for any Object, including a String. Hence, the String method is called.

<nitpick>You're referring to overloading, not overriding.</nitpick>


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37877
    
  22
That is more than a nitpick. If people think they can override static method, or fields, they will get all sorts of errors with the wrong member of a class being used.
Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

Similar topic here, you may want to check Steve's reply.

Cheers,
Claudiu
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

The example you give is overloading, not overriding.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
This is overloading, not overriding, as others have pointed out. I will add that my understanding is that, strictly speaking, there is no such thing as overriding a static method. An override happens at runtime, when the jvm, which already knows the signature of the method to be invoked (that's recorded by the compiler), figures out what object to invoke the method on. So if you had this:


Because statics are invoked on the class, there is nothing for the jvm to decide, and so no override is taking place here.


Costs matter. Justice lies in processes not outcomes. Crime is caused by criminals.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37877
    
  22
. . . but it would be different if you wrote
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static method overriding in same class
 
Similar Threads
OverLoading && Overrriding confused with output
How to store method return type in primitive
Passing null references
Garbage collection
var args method