Search code examples
javaobjectinstantiationclassloader

Object instantiation when dependency is missing (Java)


Guys, can anyone explain the following scenario:

1) Web application has module1.jar in its lib directory. There is a class A in that module:

package module1;
import module2.B;
public interface IA {   
    void methodOk() {}

    void methodWithB(B param) {}
}

package module1;
import module2.B;
public class A implements IA {
    public A() {}

    //...
void methodWithB(B param) {
    //do job on B
}
}

2) module2.jar is absent - it is not in the classpath.

3) Application is able to create objects of class A though it's missing the dependency. In application a method A.methodOk() is called.

Would be cool if you could give a reference to any spec on this. Thanks a lot.


Solution

  • Since the code is already compiled, it will not throw an error until you directly use class B. From the looks of your code, you don't actually use an instance of B for anything.