1

I have a Class object, clazz. I want to make the object of that type class.

i.e. Class clazz = MTable.getClass(tableName);

The variable clazz may be of type MOrder.class, MInventory.class, or MSalary.class. Now I want to make the object of MOrder for an instance. Without comparing the clazz with MOrder.class, how can I create object of MOrder. Is Java providing any such mechanism?

Update:

I tried this:

Class<? extends DocAction> clazz = (Class<? extends DocAction>) MTable.getClass(re.tableName);
        DocAction instance = null;
        try {
                instance = clazz.newInstance();
        } catch (InstantiationException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        }

But an exception is thrown:

java.lang.InstantiationException: org.compiere.model.MOrder

3
  • What do you want to do if you get MInventory.class or MSalary.class? Why not instantiate MOrder directly? Commented Nov 12, 2014 at 9:25
  • I have several classes; not only MInventory, MSalary or MOrder; So instantiate all those classes are difficult. Another hope is that, all those Classes are implemented an interface DocAction.class Commented Nov 12, 2014 at 9:52
  • So you want to instantiate other classes than MOrder, but not all classes? How do you decide whether to instantiate or not? Your error is probably caused by MOrder not having a default constructor by the way Commented Nov 12, 2014 at 10:04

3 Answers 3

3
Object object = clazz.newInstance();

would create the instance based on the value of clazz.

If you want to store the object in a MOrder variable, you have to check its type first :

MOrder order = null;
if (object instanceof MOrder) {
    order = (MOrder) order;
}

You can't avoid the testing of the type, since as you said, clazz may contain different Class instances.

If all of your classes implement DocAction interface, you can do the following :

Class<? extends DocAction> clazz = ...
DocAction instance = clazz.newInstace();

This would at least give you a reference of the type of the interface. You would still need to check the specific type if you want to store the instance in a more specific variable.

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

19 Comments

You can't avoid the testing of the type. Thanks. But if I have n number of class types its difficult to check each and every one. There is a hope: all those classes implements an interface called DocAction, then is there any chance of getting the corresponding method declared in DocAction?
Thanks for your thought. I am getting the java.lang.InstantiationException
I have updated the question with the code which gives me the exception.
@Sej What is the value of clazz for which you get this exception?
it is the right one : class org.compiere.model.MOrder
|
3

Try:

Object object = clazz.newInstance()

4 Comments

So we have the type of Object. right? I want the specific object of type MOrder
Just cast it? MOrder order = (MOrder) clazz.newInstance();
@Ishkafel Then you get a ClassCastException, if the class is not a MOrder.class
@Ishkafel there could be 3 kind of class
1

@Saj, the Class object is not inherited by your classes, so MOrder.class or MInventory.class can't be type of Class object. All object inherits from Object class, and even Class object inherits from Object class. So, your logic seems incorrect. Think in terms other way to know which type of Object to create.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.