package test;
public interface Virtual {
String get();
}
package test;
public class Test implements Virtual {
static { System.out.println("hello"); }
public Test() { System.out.println("world"); }
@Override
public String get() {
return "Test clazz";
}
}
//main method
...
Class<?> cls = Class.forName("test.Test", true, classLoader); // it prints "hello"
Virtual instance = (Virtual) cls.newInstance();
// this gets ClassCastException: test.Test cannot be cast to test.Virtual
The idea is get Virtual instance which is Test implementation. What is wrong with this scenario?
update: Finally I got it. It seems there was an eror in test.get().