Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface binding in Eclipse

I have the following code in Eclipse(Helios)/STS which runs and prints console output when doing a Run As> Java Application, in spite of obvious compilation issues

public interface ITest{
    String func();
}

public static class Test implements ITest{
    void printFunc(){
        System.out.println("Inside Test Function");
    }
}

public static void main(String[] args) {        
    Test test = new Test();
    test.printFunc();
}

Can anyone pinpoint the reasoning behind this Eclipse functioning.

Note: Doing a javac externally obviously fails to compile.

like image 583
Anurag Sen Avatar asked Oct 10 '12 10:10

Anurag Sen


2 Answers

Eclipse's Java compiler is designed to cope with flaky, non-compiling code. It will add whatever stuff is necessary to the code to get it to compile.

See this question What is the difference between javac and the Eclipse compiler?

like image 113
artbristol Avatar answered Sep 20 '22 16:09

artbristol


It might have been that you have coded the class successfully before the errors. Eclipse auto-compiles your file while you are coding. Just then, you happen to have errors.. then you decide to run as Java Application, Eclipse will run the most recent compiled class.

I tried your code, implemented the necessary methods to remove the errors, then removed it again to put back the errors.. sure enough, it printed out "Inside Test Function". I also tried commenting out System.out.println("Inside Test Function"); and it still printed out.

In another try, I created another class, added your code, then run (without implementing the errors to avoid auto-compiling), then it printed out an error..

java.lang.NoSuchMethodError: main
Exception in thread "main" 
like image 24
Russell Gutierrez Avatar answered Sep 21 '22 16:09

Russell Gutierrez