I am following a beginner's java book and one of the projects is to pick an error from commonly thrown errors and try to do what you can to induce the error.
I picked InstantiationError and am up to the level of knowing you can't instantiate abstract classes or interfaces. The docs says the error is usually caught by the compiler but can be thrown at runtime if the definition of a class has incompatibly changed.
I don't know how a class definition could be changed while the program is running and am looking for some tips on where I should read further. Thanks!
Class InstantiationErrorThrown when an application tries to use the Java new construct to instantiate an abstract class or an interface. Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.
Instantiation: Creating an object by using the new keyword is called instantiation. For example, Car ca = new Car(). It creates an instance of the Car class.
The InstantiationException is a runtime exception in Java that occurs when an application attempts to create an instance of a class using the Class. newInstance() method, but the specified class object cannot be instantiated.
Instantiate in Java means to call a constructor of a Class which creates an an instance or object, of the type of that Class. Instantiation allocates the initial memory for the object and returns a reference.
I don't know how a class definition could be changed while the program is running
It can't change while it's running but it can change after you've compiled.
For example, try this:
// In Test.java
public class Test {
public static void main(String[] args){
System.out.println(new Foo());
}
}
// In Foo.java
public class Foo {
}
Compile:
javac Foo.java Test.java
Run:
java Test
// Some output like this...
Foo@1d6535bf
Now change Foo.java
like this:
// No parameterless constructor any more!
public class Foo {
public Foo(int x) {
}
}
Recompile just Foo.java
:
javac Foo.java
And rerun Test:
Exception in thread "main" java.lang.NoSuchMethodError: Foo: method <init>()V
not found
at Test.main(Test.java:3)
This is not what I'd call a "commonly thrown error" though.
Note that that's not InstantiationError
- but you can change Foo.java
again, to:
public interface Foo {
}
Again, just recompile Foo.java
and this time you'll get:
Exception in thread "main" java.lang.InstantiationError: Foo
at Test.main(Test.java:3)
Maybe when you instantiate an abstract class at runtime using reflection.
Code sample:
public abstract class MyAbstractClass{
}
public class MyMainClass() {
public void test(String[] args) {
this.getClass().getClassLoader().loadClass("MyAbstractClass").getConstructors()[0].newInstance();
}
}
Have a look at the Reflection API, which offers a way to instantiate a class by name.
public void throwError() {
AbstractType type = this.getClassLoader().newInstance("my.abstract.Type");
}
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