3

When the following java code is executed in eclipse, it gives the correct output (i.e. prints 'Class B'), but according to java specification, the code cannot be compiled (since the super class constructor requires an int argument and the constructor provided by the compiler to class B includes a call to the super class no arg constructor, which is not defined), and when try to compile the file using javac command in command line, it only compiles the super class i.e. class A and fails with the following compile error:

B.java:8: cannot find symbol
symbol  : constructor A()
location: class A
public class B extends A {
   ^
1 error

Following is the java file content:

class A {

    public A(int x) {
        //
    }
}

public class B extends A {

    public static void main(String[] args) {
        System.out.println("Class b");
    }
}

Can someone explain how eclipse executes the main method without bieng able to compile the class. Thanks in advance.

UPDATE

I deleted the .class file creatd by eclipse in the bin folder and tried to execute the class, and it could not be compiled, therefore didn't run. But when I created a source file with the public class name and saved without any content, and then added the above content, it worked. If eclipse uses the previously compiled version to execute, how could it give the output of the main method, since it did not have the main method at the time of success full compilation?

Dilini Rajapaksha
  • 2,610
  • 4
  • 30
  • 41

3 Answers3

5

Actually, eclipse tries to compile the class and its not able to compile due to build errors, when you simply run those files it executed the previously compiled class, that were successfully compiled earlier (eclipse tries to compile and build the files on every save as per generic settings).

in order to resolve you can try clean the project before running, while in case of javac, it simply compiles ,

Akash Yadav
  • 2,411
  • 20
  • 32
  • 1
    Thanks for the answer, eclipse shows the compile error, but my question is how eclipse excutes the code and give output, despite having compile errors. – Dilini Rajapaksha May 11 '12 at 06:55
  • 3
    its based on the previous compiled version of the file, it will only replace those old class files after the successful compilation – Akash Yadav May 11 '12 at 06:56
  • Tried cleaning the project, but it still gives the same output, and also, I created the source file and saved at the end (after entering all the code) in eclipse, therefore it is impossible to have a previously compiled version of the class. – Dilini Rajapaksha May 11 '12 at 07:05
  • But when we do the same it reports the problem , am not able to run at all, could you try creating a fresh workspace and see if it works there – Akash Yadav May 11 '12 at 07:08
  • According to the update to my question, what I don't understand is how eclipse inserts new parts of source code to a previously compiled class. – Dilini Rajapaksha May 11 '12 at 07:37
  • it doesn't add parts , it simply replaces those files with brand new ones after sucessfull compilation – Akash Yadav May 11 '12 at 07:38
  • Please see my update. How could eclipse give the output of the `main` method with compilation errors, when last successfull compilation was only the source file without the main method? – Dilini Rajapaksha May 11 '12 at 07:43
2

Eclipse uses its own Java compiler (called ecj) instead of javac. This compiler allows (partial) compilation of broken source files.

Community
  • 1
  • 1
lxgr
  • 3,719
  • 7
  • 31
  • 46
1

Eclipse shouldn't do that. It leads to ambiguous results. If a class couldn't be compiled, then its corresponding .class file should be removed.

If you check the bin folder, B.class and A.class are present. Not sure why it creates B.class even though B doesn't compile

Ravi
  • 545
  • 3
  • 5