1

The below code gets compiled successfully, wherein inside some class, I have written two methods with same name & same type signature. Am using Eclipse Oxygen 4.7.0 and I can see error Duplicate method show(int,int) shown in red. But when I compile the code, its getting compiled successfully with correct output. When I run the same code in command prompt using javac its validly not getting compiled.

package oops2;

class A6
{
int i, j;

void show(int i, int j)
{
    System.out.println(" i & j : " +i + " " +j);
}

void show(int k, int l)
{
    System.out.println("override or not");
}

void show(String s)

{
    System.out.println("Entered str is "+s);
}
}

public class OverrideNoInherit 
{
public static void main(String[] args) 

{
    // TODO Auto-generated method stub
A6 a = new A6();
a.show(20, 30);
a.show("this is it");   
}

}

Here, I want to mention that when I click on run on Eclipse, I get a pop-up saying

Errors exist in this project, proceed with launch?

Now, didn't this error mean that there are other classes in same project which had errors & thus did not get compile. I don't think this meant that despite there being errors in code, the programs would get compiled. Then what would be difference between warnings & errors?

CKE
  • 1,533
  • 19
  • 18
  • 29
manisha
  • 11
  • 2
  • 2
    That's how the Eclipse compiler works. For all kinds of errors. It compiles the classes anyway, with code throwing a runtime error replacing the code that doesn't compile. I gess it helps with incremental compilation. But I agree that it sucks, especially for newbies who try to run code despite the fact that it doesn't compile without error. – JB Nizet Jun 24 '18 at 16:06
  • Thanks, however what I meant was, apart from compiling am getting output from the code in eclipse, the output I got was this:- 'i & j : 20 30 Entered str is this is it' How the program got successfully executed – manisha Jun 24 '18 at 16:18

1 Answers1

2

Eclipse always compiles classes. The body of methods containing errors are replaced by a method that throws a java.lang.Error containing the compile errors of that class. This is actually helpful for test driven development that allows you to start tests on classes that have compile errors. The methods that are compilable can be tested even if some other parts are currently containing errors.

You've got a special case here. The compiler is able to compile the first occurance of the method but later comes across the duplicate method that can't be added to the class. Because the first method is already compiled and when you enforce the starting of the class despite the warning you see the regular method that was successfully compiled in action.

Lothar
  • 5,323
  • 1
  • 11
  • 27
  • Thanks @Lothar ..If I understand it correctly, it means, Eclipse will always ignore Compile-time errors & program wont be executed only when there will be a run-time error... since the program I mention gets compiled and got executed successfully with an output. Also, if eclipse always compiles the classes, then why below code is not executed successfully. ` int st = "this is how" ` this is a compile time error but when I run it, in the result I get "could not convert from String to Int" – manisha Jun 25 '18 at 08:04
  • Eclipse only "ignores" compile errors if they can be ignored, e.g. if you have a mismatch between java class name in the source and filename I doubt that you will end up with a class-file. Concerning your second question: That's the normal result (your example is a special case). As I said in my answer, the content of the method containing a compile-error is a `throw new Error(compileErrorMessages)` which is exactly what you can see as a result of your second example. – Lothar Jun 26 '18 at 09:36