When I am creating my project using the Eclipse IDE it is generating a class file even when there is a syntax error in my code?
class Test {
public void test(String value) {
System.out.println("TEST CALLED WITH VALUE " + value);
}
}
class Abc {
Test obj = new Test();
public String firstCallToMethodFromTest() {
System.out.println("FIRST CALL TO THE METHOD FROM TEST CLASS");
String result = obj.test("TEST");
return result;
}
public String secondCallToMethodFromTest() {
System.out.println("SECOND CALL TO THE METHOD FROM TEST CLASS");
String result = obj.test();
// There is no such method in test class i.e source code error
return result;
}
}
Method firstCallToMethodFromTest
is called as an action method from my Struts action. How does Eclipse make it possible to compile code for the Abc
class where there are syntax errors in my source code file?
class" file is created as a result of successful compilation by the Java compiler from the ". java" file. Each class in the . java file is compiled into a separate class file if the ".
Click on Window > Preferences > Java > Decompiler to configure which default class decompiler to decompile Java class.
A . java file contains your Java source code while a . class file contains the Java bytecode produced by the Java compiler.
There is a reason. It allows applications with compilation errors to be run (sort of!). What the compiler does is to create stub methods for any methods that it cannot compile due to errors in the source code. If the application calls one of these stub methods, you get a runtime exception that says that the method had a compilation error.
IMO, this "feature" is mostly harmful ... and it can be very confusing for Eclipse newbies. However, it can be useful for people who want to runtests, etc on partly written classes.
IIRC, there is a checkbox in the Run dialogs that allows you to enable / disabling running applications that have compilation errors. (I always disable it!)
UPDATE
This behaviour is Eclipse specific. It is controlled by a setting in the "Window > Preferences > Run/Debug > Launching" preference panel.
Because you can run and debug a class that has only been compiled partially, as long as you only move through the code parts which compiled without error. If your control flow comes to the place with the compilation error, an exception will happen at runtime.
Just remember if you have ever changed code directly during debugging (hot code replacement): Many IDEs will even warn you that under some conditions you are partially removing existing code, but you still want to continue that exact same debugging session, so this feature is really needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With