Results 1 to 7 of 7
  1. #1
    Kmsitu_bcn is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default 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;
    Thanks.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,809
    Rep Power
    19

    Default 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.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,346
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,809
    Rep Power
    19

    Default 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...

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,346
    Blog Entries
    7
    Rep Power
    20

    Default Re: Use the value of a string as a variable/atribute

    Quote Originally Posted by Tolls View Post
    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...
    Yep, true, but Reflection is like surgery and you don't want to dig through intestines to find a member of an object ;-) Reflection ruins all the object oriented stuff and all the rules of the game.
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,809
    Rep Power
    19

    Default Re: Use the value of a string as a variable/atribute

    Well, yes.
    Bad reflection...naughty!

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,346
    Blog Entries
    7
    Rep Power
    20

    Default Re: Use the value of a string as a variable/atribute

    Quote Originally Posted by Tolls View Post
    Well, yes.
    Bad reflection...naughty!
    It needs a bare bottom spanking!

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Convert variable name to string.
    By MHardeman25 in forum New To Java
    Replies: 9
    Last Post: 08-17-2010, 09:18 PM
  2. how to Parse int to a string variable (pls hlp)
    By raj reddy in forum Threads and Synchronization
    Replies: 5
    Last Post: 06-10-2008, 06:32 AM
  3. Help with variable assigment to String
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:43 AM
  4. Replies: 1
    Last Post: 08-03-2007, 10:52 AM
  5. String Variable
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-06-2007, 04:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •