Skip to main content

How to assign value is variable

1 reply [Last post]
haribabu_2006
Offline
Joined: 2008-09-04

Hi,

I want to know how i can achieve the below statement

String x = "abc";

Now i want to create an int variable which is same value as x i.e. String x="$abc"; int $abc = 5.

Please help me.

Thanks
-Hari

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tarbo
Offline
Joined: 2006-12-18

If you'd like your integer to hold the equivalent value to your String, use [url=http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String)]Integer.parseInt(x)[/url].

If you want to assign the value [i]5[/i] to a variable you address by name, you can use reflection: [url=http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getField(java.lang.String)]getClass().getField(x)[/url].[url=http://java.sun.com/javase/6/docs/api/java/lang/reflect/Field.html#setInt(java.lang.Object, int)]setInt(this, 5)[/url].