graviton
graviton

Reputation: 463

Dynamically creating an instance of a class

In the following, what I want to happen is that w.someMethod() returns an instance of SomeSubClass. Unfortunately it does not work. I believe the code will explain it better than I can.

public abstract class SomeSuperClass {
    public SomeSuperClass(int x) {
        // do nothing
    }
    public SomeSuperClass someMethod() {
        //this line
        return (new Class()).getDeclaredConstructor(getClass()).newInstance(1));
        //this line
    }
}

public class SomeSubClass extends SomeSuperCLass {
    public SomeSubClass(int x) {
        super(x);
    }
}

public static void main(String[] args) {
    SomeSubClass w = new SomeSubClass(3);
    SomeSubClass q = w.someMethod();
}

Here is my return

SomeSuperClass.java:7: error: Class() has private access in Class
    return (new Class()).getDeclaredConstructor(getClass()).newInstance(1);
            ^
SomeSuperClass.java:7: error: incompatible types
    return (new Class()).getDeclaredConstructor(getClass()).newInstance(1);
                                                                       ^
required: SomeSuperClass
found:    Object
Main.java:4: error: incompatible types
    SomeSubClass q = w.someMethod();

To further clarify, the following code would do what I want:

public abstract class SomeSuperClass {
    public SomeSuperClass(int x) {
        // do nothing
    }
    public abstract SomeSuperClass someMethod();
}

public class SomeSubClass extends SomeSuperCLass {
    public SomeSubClass(int x) {
        super(x);
    }
    public SomeSuperClass someMethod() {
        return new SomeSubClass(1);
    }
}

public static void main(String[] args) {
    SomeSubClass w = new SomeSubClass(3);
    SomeSubClass q = w.someMethod();
}

But I would like the someMethod to be in the class SomeSuperClass

Upvotes: 1

Views: 309

Answers (3)

Russell Zahniser
Russell Zahniser

Reputation: 16364

If what you want is for this method to return a new instance of whatever class it is called on, using a constructor that takes an int, then you should do:

return getClass().getDeclaredConstructor(int.class).newInstance(1);

But it's not clear why you wouldn't just override the method to explicitly instantiate the subclass in each subclass, as JoeC suggests.

Upvotes: 2

Cyrille Ka
Cyrille Ka

Reputation: 15533

new Class is not the proper way to obtain the class object. Instead the proper way is to use the method getClass()

return getClass().getDeclaredConstructor(int.class).newInstance(1);

But you don't need to use reflection to create an object like the one you already have. Instead you can override the method someMethod the way JoeC suggest you.

Upvotes: 3

JoeC
JoeC

Reputation: 1850

Overriding the someMEthod() in your child class?

public class SomeSubClass extends SomeSuperCLass {
    public SomeSubClass(int x) {
        super(x);
    }
    public SomeSuperClass someMethod(){
        return new SomeSubClass(1);
    }
}

Upvotes: 2

Related Questions