2

Let's say we have 2 classes A and B

public class A{

    private int member1;

    A() {
        member1 = 10;
    }

    public getMember(){
        return member1;
    }

}

Class B is also on the same lines except that its member variable is named member2 and gets intitialized to say 20 inside the constructor.

My Requirement :

At runtime , I get a string which contains a className ( could be A or B). I want to dynamically create an object of this class along with invoking the constructor. How can I achieve this . I don't want to use interfaces for common functionality of above classes Morever, later on I set the properties of this raw object using Propery Builder Bean Util class based on a list of columns .

Class clazz = Class.forName("className");
Obj obj = clazz.newInstance();

How I can dynamically convert that obj to className object.

3
  • Hi there, I slightly edited your question to format your Java code better. As far as I can tell on StackOverflow code looks nicer if you select it and click on the "10010001" icon, shifting all your code to the right (it then gets formated) rather then using the xml <code> tag. Commented May 19, 2010 at 15:37
  • You can't do that, but you could have a third class, say Common (either a class or Java interface) and then have A extends Common and B extends Common or A implements Common and B implement Common and put the common functionalities in the "Common" class/interface. From your question it looks like there's shared common functionalities... Commented May 19, 2010 at 15:39
  • I don't want an interface here as the Type T classes here are domain classed much different in their behaviour . Above was just an example functionality to give an idea of what I am trying to accomplish and doesn't represent the correct state of my code. Now what ? Commented May 20, 2010 at 6:45

3 Answers 3

1

How can I achieve this . I don't want to use interfaces for common functionality of above classes

Then the answer is very simple and you won’t like it: you can’t. You want to modify the static type of the variables which is, by definition, determined at compile time. Changing it at runtime is not possible.

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

Comments

1

What do you mean with "dynamically convert"? It IS an object of type "className", stored in a variable of type Object. If you want to use it as an object of type A, you have to cast it, and for example store it in a variable of type A.

1 Comment

I meant dynamically cast that object to object of Type T . Any pointers now ?
1

Class Class has a cast method which at first sight seems to be doing just what you want. So you could try

... = clazz.cast(obj);

but what would be the return type??? It should be either A or B, but you can't declare a variable dynamically...

So I see no other way than the ugly, but tried and true

if (obj instanceof A) {
  A a = (A) obj;
  ...
} else if (obj instanceof B) {
  B b = (B) obj;
  ...
}

Note that if with bean introspection, you can always see the actual dynamic type and internals of the object, so I see not much point trying to get a static reference of the right type to it.

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.