Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiation error

Tags:

java

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!

like image 714
rjones Avatar asked Feb 12 '13 19:02

rjones


People also ask

What is instantiation error in Java?

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.

What is instantiation example?

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.

What is instantiation exception?

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.

What happens during instantiation?

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.


3 Answers

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)
like image 69
Jon Skeet Avatar answered Sep 21 '22 14:09

Jon Skeet


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();
    }
}
like image 41
axelrod Avatar answered Sep 24 '22 14:09

axelrod


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");
} 
like image 23
Peter Bratton Avatar answered Sep 24 '22 14:09

Peter Bratton