views:

81

answers:

3

Hi,

I have got a bean class names as "Bean1". In my main method i have got a string containing the name of the variable. String str= "Bean1"; Now how can i use the String variable to get the class and access the Bean properties. I am new to Java. please help.

+4  A: 

You should use Java Reflection API:

Class c = Class.forName("package.name.Bean1");

Then you may use c.newInstance() to instantiate your class. This method uses constructor which does not require parameters.

See details here: http://download.oracle.com/javase/tutorial/reflect/

Kel
he probably also needs an instance of Bean1
cherouvim
The functionality that am trying to implement is that "There is a Inetrface and related Bean classes created at the run time of an application. I am able to get the the path where the bean class and interface is located. Now i have got a class "SrcInfo" where the mehtod list and the parameters of the Bean is provided as class variables. Now i have to dynamically create a java class where i need to create a method that takes Bean object as an argument and returns a HashMap by manipulating the data contained in Bean object. Thus i have got the bean class name as String variable.
MANU SINHA
@MANU SINHA: you should include this information at the question (edit it, please). This is important information for getting a helpful answer
Tomas Narros
Frankly, I don't understand complete workflow you are going to implement. If you need to instantiate class by name - use solution provided by me or by Tomas Narros. If you need to access instance fields and convert them to HashMap - use Class.getDeclaredFields() and Field.get() method. You may see corresponding JavaDoc for details (http://download.oracle.com/javase/6/docs/api/java/lang/Class.html), or ask one more question for further details.
Kel
+1  A: 

Duplicate of Does Java support variable variables?

Java doesn't support dynamically getting a variable based on a string of its name (also known as variable variables). There's likely a different way of doing what you're trying to do, such as using a Map object to map names to beans. If you edit your question to explain what you want to do in a bit more detail, we might have some more concrete answers.

(On the other hand, if the question was about a class called Bean1, then Kel's right.)

Nick
+2  A: 

Step by step:

//1. As Kel has told you (+1), you need to use 
//Java reflection to get the Class Object.
Class c = Class.forName("package.name.Bean1");

//2. Then, you can create a new instance of the bean. 
//Assuming your Bean1 class has an empty public constructor:
Object o = c.newInstance();

//3. To access the object properties, you need to cast your object to a variable 
// of the type you need to access
Bean1 b = (Bean1) o;

//4. Access the properties:
b.setValue1("aValue");

For this last step, you need to know the type of the bean, or a supertype with the properties you need to access. And I guess that you don't know it, if all the information you have on the class is a String with its name.

Using reflection, you could access the methods of the class, but in this case, you would need to know the names and the input parameter types of the methods to be invoked. Going ahead with the example, change the steps 3 and 4:

// 3. Get the method "setValue1" to access the property value1, 
//which accepts one parameter, of String type:
Method m=c.getMethod("setValue1", String.class);

// 4. Invoke the method on object o, passing the String "newValue" as argument:
m.invoke(o, "newValue");

Maybe you need to rethink your design, if you don't have all this information avalaible at runtime.

Tomas Narros