Object Instantiation from User Input

How can I create an object that is the type a user has passed in? Here is an example of what I mean:

import java.util.*;
public class ObjInit {
  public Object initObjOfType(String type) {
    Object obj;
    if(type.equals(/*Type of Object*/)) {
      obj = new /*Type of Object*/();
    }
    return obj;
  }
}

A problem I encountered when attempting this is that the type of object is casted to an object, and I do not want that. I want to make it so no casting is needed so it is all automated.

You can use the reflection API.
There is a good example here:

1 Like

Being more specific by adapting your example:

import java.util.*;

public class ObjInit {
  public Object initObjOfType(String type) {
    Object obj = null;
    try {
      Class<?> clazz = Class.forName(type);
      obj = clazz.getDeclaredConstructor().newInstance();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return obj;
  }
}

I am not sure I am using this correctly. I used your code, and It gives me an error, saying myObject can not use nextLine() because it is of type Object.

Object myObject = o.initObjOfType("Scanner");
myObject.nextLine();

Hello, @LegoWizardCode! Welcome to the forums!

If you want to create an object of the type specified by a user in Java, you can use reflection to accomplish this. Here is an example implementation of your initObjOfType method using reflection:

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Main {
  public Object initObjOfType(String type) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    Class<?> clazz = Class.forName(type);
    Constructor<?> constructor = clazz.getConstructor();
    return constructor.newInstance();
  }

  public static void main(String[] args) {
    // your code here
  }
}

}

}

Yes and again, if this does not work im gonna
:sob:

1 Like

This works, but is there a way to make it so it returns the object as the initialized type rather than type object? (Without casting)

Since the type is being determined at runtime, it’s not possible for the method to return a value of that type without casting.

Buuut, you can use Java’s generics to achieve something similar

Like this:

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Main {
  public <T> T initObjOfType(Class<T> clazz) 
    throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
      
    Constructor<T> constructor = clazz.getConstructor();
    return constructor.newInstance();
  }

  public static void main(String[] args) {
    try {
      String str = new Main().initObjOfType(String.class);
      // Use str...
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

2 Likes

This is the solution. Thank you.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.