Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By Norm
  • 2 Post By pbrockway2

Thread: Use variable value to name object.

  1. #1
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default Use variable value to name object.

    Hello everyone,
    I am trying to create instances of an object I call CalendarEvent. However, I want to name the instances according to a dynamic variable. So I want to create object instances that use the value of a variable as a name.

    For example, in PHP, if you write

    PHP Code:
    <?php
    
    $text = "hey" ;
    
    $$text = "result" ;
    
    ?>
    You are actually assigning "result" to the variable $hey. However, in Java, there are no dollar marks, so this is harder.

    Can you name a way of doing this in Java?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,450
    Rep Power
    25

    Default Re: Use variable value to name object.

    In java and most compiled languages, all variable names are created when you type them into the source. Interpreted languages can do things like that as they execute.
    You can get a similar effect by using a Map.
    Fubarable likes this.

  3. #3
    funstein is offline Member
    Join Date
    Aug 2011
    Posts
    24
    Rep Power
    0

    Default Re: Use variable value to name object.

    Can you show me how?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,450
    Rep Power
    25

    Default Re: Use variable value to name object.

    Do a search for the java Map. There are many code samples on the forum.
    Also go to this site and Find Map. It has lots of doc on how to use Maps.
    The Really Big Index

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Use variable value to name object.

    Can you show me how?
    Can you tell us why?

    I ask this not to be combative, but because maybe whatever it is that you're trying to achieve can be done most naturally in Java some other way. Possibly with Map (in the collection classes already linked to), or maybe a humble array, or a fancy embedded interpreter, or just some change in how the task is approached to eliminate dependence on what word is used as a variable.
    Norm and Fubarable like this.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Use variable value to name object.

    To get overly philosophical on this subject,... I think that every programming language has its own philosophy and modus operandi partly dictated by culture and mostly dictated by the limitations and the powers of the language, and I think that the original poster's problems are arising from a conflict between the philosophy of PHP programming and Java programming. Neither is right nor wrong, but you can't apply all PHP concepts directly to Java and visa versa.

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Use variable value to name object.

    To add to what others have said: in a compiled language like Java, variables no longer have names once the program is compiled and running. A variable you call "hey" in the source is known to the running program only as a memory address. So strictly speaking, the question as you've asked it makes no sense. But as Norm said, you can use a Map to associate an object with a string input by a user. If that's actually what you want to do.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Variable for object refference?
    By aianta in forum New To Java
    Replies: 3
    Last Post: 08-22-2011, 05:56 PM
  2. pass a variable from one object to another
    By kev670 in forum New To Java
    Replies: 3
    Last Post: 11-25-2010, 01:20 AM
  3. same object variable t and same methods is calling
    By javastuden in forum New To Java
    Replies: 1
    Last Post: 11-24-2009, 04:10 AM
  4. Object name by string variable?
    By zerkz in forum New To Java
    Replies: 4
    Last Post: 10-14-2009, 07:16 AM
  5. variable to accept a single object
    By Rgfirefly24 in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:41 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
  •