8

I'm trying to create an object of a class dynamically by sending the class name as a string. I have searched in all the java forums but i couldn't get the answer which i wanted. Here is my requirement, i have a class with name Agent,

package somePack;

public class Agent{
  private String Id;
  Private String Name;
  public String getId() {
    return this.id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getName() {
    return this.name;
  }
  public void setName(String name) {
    this.name = name;
  }
}

package somePack;

public class Employee{
  private String Id;
  Private String Name;
  public String getId() {
    return this.id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getName() {
    return this.name;
  }
  public void setName(String name) {
    this.name = name;
  }
}
public class create{
   public static void main(String[] args){
      newCreate("Employee");
      newCreate("Agent");
   }
   public static void newCreate(String name){
      String path="somePack."+name;
      Class cls=Class.forName(path);
      System.out.println("Class Name " + cls.getName());
  }
}

Now my question is, cls.getName() gives me the class name, but now i want to create the object for that class i.e., for Employee class and Agent class respectively, but how can i create the object for them? The string that is passed may be something else from the other method, how can i create an object for such kind of things.

Can anyone help me....

Thank you in advance,

1
  • 2
    Class.forName() Lookup for this. Commented Oct 10, 2012 at 5:57

3 Answers 3

9

but now i want to create the object for that class

Then use the Class.newInstance() method if you never need to handle constructors with arguments, or use Class.getConstructor(...) or Class.getConstructors() otherwise. (Call Constructor.newInstance() to invoke the constructor.)

Sign up to request clarification or add additional context in comments.

1 Comment

hi Jon , I want to create an object for a class of the string variable's value. If i don't know it as "Agent", it might be something else, then how can i generate it dynamically... i hope you got my question...
7

Use Class#newInstance()

Object o = Class.forName(path).newInstance();

You can then cast this to Agent.

Agent agent = (Agent) Class.forName(path).newInstance();

2 Comments

If i use Object o = Class.forName(path).newInstance(); then how can i set and get the values for the class.
Hi Matthew, thank you for your reply, but if there are some 50 of classes and based on some criteria i send the related string as the parameter, then how can i know which class object to be created dynamically. I.e., now i'm passing it as "Agent", next if i may pass "Employee" then how this method will be helpful for me to generate the string variable's value as an object
5

On the grounds that the class has a default constructor (ie no parameters), you can simply call Class#newInstance.

If it has parameters, it becomes a little more complicated.

You'd have to get a reference to the appropriate Constructor via Class#getConstructor, passing in the expected parameter class types.

Once you have that, you can call Constructor#newInstance passing it the appropriate objects

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.