• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static method calling

 
Subhadeep biswas
Greenhorn
Posts: 14
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic