| Author |
ExamLab: This snippet compiles, but the other one doesn't. Why?
|
Harry Henriques
Ranch Hand
Joined: Jun 17, 2009
Posts: 206
|
|
|
 |
Salil Vverma
Ranch Hand
Joined: Sep 06, 2009
Posts: 219
|
|
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.
|
Regards
Salil Verma
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
This has been asked multiple times before. Try to search first before asking a question. Here are a few results which contain similar questions
http://www.coderanch.com/t/454808/Programmer-Certification-SCJP/certification/overloading
http://www.coderanch.com/t/455690/Programmer-Certification-SCJP/certification/confusing-var-arg-constructors
http://www.coderanch.com/t/453313/Programmer-Certification-SCJP/certification/var-arg
http://www.coderanch.com/t/449723/Programmer-Certification-SCJP/certification/WIDENING-VAR-ARG
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Harry Henriques
Ranch Hand
Joined: Jun 17, 2009
Posts: 206
|
|
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
Joined: Sep 06, 2009
Posts: 219
|
|
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
Joined: Jun 17, 2009
Posts: 206
|
|
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
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16692
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
|
|
subject: ExamLab: This snippet compiles, but the other one doesn't. Why?
|
|
|