• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ExamLab: This snippet compiles, but the other one doesn't. Why?

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator








 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Harry,
At first compiler checks the availability of exact matching function definition. If it does not find, then checks for other possible implementations to which the function call can be mapped to. In the first two examples, it finds the exact match so it does not give any error and runs fine. In the last scenario it sees that there is no definition of function and as an alternative this call can be fulfilled through the other two functions..

This is where the ambiguity comes and compiler gives compilation error.

You can see the same thing happening if you call the function using int argument . like in below code



it would give compilation error at given two line of codes


I hope this would clear your doubt.


 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Salil Verma wrote:



Thanks. I thought that "static" methods behaved differently than "instance methods". The following code snippet compiles without problem minus the "static" modifiers.

Harry


 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Harry,

It is not about static of instance methods but about the ambiguity that it faces while uniqualy identifying which function to call. if you make then instance methods even then you would get the same compilation error like in below code snippet.



you will get the compilation error on the same lines which are -



I hope this would clear your doubt.
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:This has been asked multiple times before. Try to search first before asking a question. Here are a few results which contain similar questions



Ankit,

This blog is not only about asking and answering questions. I probably could find the answers to my questions on www.google.com just as easily as www.javaranch.com. The point of asking questions on this blog is to have an on-going conversation with other people who are interested in the Java language. Maybe I could find the answers in a book, but it's more fun to go back and forth with other developers about these questions. I don't want to read a lot of message threads from five years ago looking for the one that answers my question. The learning experience is really a "live" experience here.

Thanks,
Harry
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The point of asking questions on this blog is to have an on-going conversation with other people who are interested in the Java language. Maybe I could find the answers in a book, but it's more fun to go back and forth with other developers about these questions.



This can go both ways... If the topic is general (and open enough) to get into an interesting conversation, then yes. If the topic is complex enough to discuss the many nuances, then yes.

If the topic is looking for a straight answer (and not an open ended discussion), on a relatively simple topic, or on something that comes up often, then no. Not doing a quick check prior to asking the question is just being lazy, and shows a disregard for other people's time.

These examples are, of course, the two extremes. I am not commenting on where this topic fits.


I don't want to read a lot of message threads from five years ago looking for the one that answers my question. The learning experience is really a "live" experience here.



It may be boring, but reading books, reading tutorials, etc. etc. is part of research, which is an important skill needed by any professional programmer.

Keep in mind that you are asking very simple (classroom) questions to a forum. What will happen when you run into something hard in a real project? Say debugging a huge program that has been around for decades, and worked upon by hundreds of developers (many of which changed jobs) -- there might not be a forum of people that can help you.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic