-1

I need help I need to know if Java allows to create an object dynamically, using the value of a variable.

Example

// I have 2 classes:

     public class Audit {
     private Long idAudit
     // constructors, get and set
     }
    
     publish class Example {
     private Long idExample
    // constructors, get and set
     }
-------------------------------------------------- -----
// create Audit and Example class object
Audit objAudit = new Audit ();
Example objExample = new Example (); 

my question is the following can you create an object either of type Audit or example using the value of a variable as I try to do in the following example. Example:

String className = "Audit"; // variable that contains the class of the Object to create
className auditObject = new ClassName (); // I use the variable classname to create the desired object

Clearly I get an error trying to create the object that way, my question is can I create an object dynamically or some other option to try to achieve what I need. Thank you

3
  • You "can" but it's not for beginners. There's a lot of problems involved that require careful thought. My question is "Why do you want to do this?" There's usually better ways of solving a problem. Commented Jul 5, 2021 at 14:31
  • Can you? Sure, that's what Class.forName() is for. Should you? Maybe, but... if you're asking this question, it may be an XY problem. Commented Jul 5, 2021 at 14:32
  • You can use Reflection, but that doesn't mean you should. Commented Jul 5, 2021 at 14:33

2 Answers 2

0

Reflection is what you are searching for

    final String className = "Audit";
    final Class<?> clazz = Class.forName(className);
    final Object o = clazz.getConstructor().newInstance();
Sign up to request clarification or add additional context in comments.

Comments

0

There are several ways you can do this.

One is called reflection, and I will let you read about it on your own.

The other one is called a factory pattern. You can create a class called ObjectFactory. in that class you will have a method public Object createObject(String type).

In the method you can check if the type you received is one of your known types, and based on the type you can create the instance of the correct class. It is better of your classes implement the same interface. Then of course your method would return the instance of that interface (or a common base class).

2 Comments

Mentioning Factory Pattern seems to be overkill here as it will also need a mechanism to create object which is the actual question here.
@SaurabhSingh I don't see it this way. Actual creation is just using "new" operator for the appropriate class. choosing which class based on a String parameter is the point of a Factory pattern in its simplest understanding

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.