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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "static method calling" Watch "static method calling" New topic
Author

static method calling

Subhadeep biswas
Greenhorn

Joined: Apr 30, 2012
Posts: 14

method calling


after running why there is ambiguity @ call(1,2) as i am calling the static method not the instance method...please help
Emanuel Borsoi
Greenhorn

Joined: Mar 27, 2012
Posts: 15
Subhadeep biswas wrote:
after running why there is ambiguity @ call(1,2) as i am calling the static method not the instance method...please help


The compiler sees an ambiguity, since the name and signature are similar, standing on the passed parameters.
if e.g you write call(1f,2); it tells that it's not possible to access the method. If you write call(1,2f); it is fine.
Pondurai Singh
Ranch Hand

Joined: Jun 27, 2011
Posts: 35
The call() method with an int and a float argument (in that order) executes the first method, and another call() method with a float and an int argument executes the second method. With each of these runs, the compiler can find an exact match for the arguments you send. However, if you run call() using two integer arguments, as in cal1(1, 2);, an ambiguous situation arises because there is no exact match for the method call. Because two integers can be promoted to an integer and a float (thus matching the first all() overloaded method),or to a float and an integer (thus matching the second version), the compiler does not know which one of the call() method to use and the program does not compile.

So the compiler Show error message
Main.java:9: reference to call is ambiguous, both method call(int,float) in Main and method call(float,int) in Main match call(1,2);


Software Developer, Oracle Java Certification Training Labs at EPractize Labs.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Subhadeep biswas wrote:after running why there is ambiguity @ call(1,2) as i am calling the static method not the instance method...

Because plainly you're not - or at least the compiler can't work out that you're not.

I wouldn't be at all surprised if you replaced that line with
Main.call(1, 2);
that you wouldn't get the error (but I'm not absolutely sure).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Winston Gutkowski wrote:
Subhadeep biswas wrote:after running why there is ambiguity @ call(1,2) as i am calling the static method not the instance method...

Because plainly you're not - or at least the compiler can't work out that you're not.

Ah, I think I see where you're coming from now Subhadeep: you're assuming that because you're issuing the call from a static method, the compiler should be able to work out that the only possible method that you can be referring to is the static one.

Sadly, compilers aren't always as smart as we are (although conversely, they're often a lot smarter ). A trawl of the JLS might give you the exact reason, but you may have a lot of reading to do.

My advice: Always use class.method() for static calls; it makes things a whole lot easier.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static method calling