Results 1 to 7 of 7
- 12-01-2011, 02:10 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
Use the value of a string as a variable/atribute
Hello!
I have a little question and cant find the solution, so if someone can please help me, I'll appreciate it.
I would like to use the value of a string as a atribute of a class.
Incorrect Example:
String str = "atributeName";
className.str = X;
- 12-01-2011, 02:18 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Use the value of a string as a variable/atribute
Reflection, though that is usually a Bad Idea outside of certain frameworks.
And this looks like it may well be one of those Bad Ideas.
- 12-01-2011, 04:29 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Use the value of a string as a variable/atribute
Only interpreters keep the identifier names during runtime (e.g. most have an "eval" funtion/method); compilers don't pass their symbol tables (where the identifier names are stored) over to the runtime; in fact it may even be impossible to do so. Maybe a Properties object or a Map<String, Object> can be a solution?
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 12-01-2011, 05:00 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Use the value of a string as a variable/atribute
If you know thename of an attribute of a class I thought you could use reflection to access it?
Not that I've ever bothered too much with it...
- 12-01-2011, 05:32 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Use the value of a string as a variable/atribute
Build a wall around Donald Trump; I'll pay for it.
- 12-01-2011, 06:03 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Use the value of a string as a variable/atribute
Well, yes.
Bad reflection...naughty!
- 12-01-2011, 06:34 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
Convert variable name to string.
By MHardeman25 in forum New To JavaReplies: 9Last Post: 08-17-2010, 10:18 PM -
how to Parse int to a string variable (pls hlp)
By raj reddy in forum Threads and SynchronizationReplies: 5Last Post: 06-10-2008, 07:32 AM -
Help with variable assigment to String
By silvia in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:43 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 11:52 AM -
String Variable
By Eric in forum Advanced JavaReplies: 1Last Post: 06-06-2007, 05:30 AM
Bookmarks