-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

Kenny Cruz
  • 47
  • 1
  • 8
  • 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. – markspace Jul 05 '21 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. – Dave Newton Jul 05 '21 at 14:32
  • You *can* use [Reflection](https://stackoverflow.com/questions/4767088/creating-an-instance-from-string-in-java), but that doesn't mean you should. – Lino Jul 05 '21 at 14:33

2 Answers2

0

Reflection is what you are searching for

    final String className = "Audit";
    final Class<?> clazz = Class.forName(className);
    final Object o = clazz.getConstructor().newInstance();
krankkk
  • 124
  • 1
  • 5
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).

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • Mentioning Factory Pattern seems to be overkill here as it will also need a mechanism to create object which is the actual question here. – Saurabh Singh Jul 05 '21 at 16:45
  • @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 – Michael Gantman Jul 05 '21 at 17:06