Results 1 to 7 of 7
- 11-06-2011, 01:42 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 24
- Rep Power
- 0
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
You are actually assigning "result" to the variable $hey. However, in Java, there are no dollar marks, so this is harder.PHP Code:<?php $text = "hey" ; $$text = "result" ; ?>
Can you name a way of doing this in Java?
- 11-06-2011, 03:05 PM #2
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.
- 11-06-2011, 03:19 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 24
- Rep Power
- 0
Re: Use variable value to name object.
Can you show me how?
- 11-06-2011, 03:24 PM #4
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
- 11-06-2011, 11:42 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Re: Use variable value to name object.
Can you tell us why?Can you show me how?
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.
-
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.
- 11-07-2011, 04:01 AM #7
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
-
Variable for object refference?
By aianta in forum New To JavaReplies: 3Last Post: 08-22-2011, 05:56 PM -
pass a variable from one object to another
By kev670 in forum New To JavaReplies: 3Last Post: 11-25-2010, 01:20 AM -
same object variable t and same methods is calling
By javastuden in forum New To JavaReplies: 1Last Post: 11-24-2009, 04:10 AM -
Object name by string variable?
By zerkz in forum New To JavaReplies: 4Last Post: 10-14-2009, 07:16 AM -
variable to accept a single object
By Rgfirefly24 in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:41 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks