• Post Reply Bookmark Topic Watch Topic
  • New Topic

Do I have the chance to use a String Constant as a Varible Name?  RSS feed

 
Oliver Lee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As title,is it possible to use a String Constant as a String Varible Name?
 
Wiyanto Ngasinur
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no since String is one of keyword on java
 
ilkin esrefli
Greenhorn
Posts: 28
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you may write this
String String = "";

but it is not recommended..
[ December 28, 2007: Message edited by: ilkin esrefli ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you cannot use a string constant as a variable name. Why would you want to do that?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wiyanto Ngasinur:
no since String is one of keyword on java

"String" is not a keyword. It's just a class name.

But I'm not sure I understand the original question.
 
Oliver Lee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i want is like this:

String firstvar= "mysecondvar";

how to define a new String variable,use firstvar's content as its variable name.

like

String mysecondvar = "new string";(here i cant use mysecondvar directly,because i only know a variable firstvar)

by the way,forgive my poor english!thanks a lot!
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's fine Oliver. It would be better if you could make the people understand what you intend to convey!

I do NOT think it is possible to get the content of the string and set it as another variable's name. But Reflection could help you though.
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some time i also get some cases that i need to create variable. like that way.. but still i not found any way to do like this.

If any one know how it`s possible then please reply.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Oliver Lee:
what i want is like this:

String firstvar= "mysecondvar";

how to define a new String variable,use firstvar's content as its variable name.

If you think a little bit longer about this, you'll see that it makes no sense to try to do this.

You write the source code of a program first. Once the source code is finished, you compile and run the program.

What you want to do is create a new variable at runtime. But that's too late - you declare variables while you write the program. When the program is running, you can't change the source code anymore; in fact, the source code isn't even relevant anymore at runtime (the compiler converted it to Java byte code, and that's what you're running).

Maybe what you want is something like a Map. Lookup Map and HashMap in the Java API documentation. Here's a short example.
See also: The Java Tutorial - Collections

[ January 02, 2008: Message edited by: Jesper Young ]
 
Oliver Lee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well,i am very appreciate all of you!Maybe i can only handle this in another way,because it is useful for me to to so. for example,i have a method,which need a var name as a parameter to be passed.
[ January 03, 2008: Message edited by: Oliver Lee ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method that takes a variable name as a parameter? And what is the method going to do with that variable name? Can you explain in more detail, with a code example, what you're trying to do?
 
Bert Bates
author
Sheriff
Posts: 8954
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I remember hearing that JavaScript lets you do something like this...it could be that a JavaScript book might explain why you'd want to?

In any case, it's definitely NOT on any version of the SCJP exam, so let's slide it...
[ January 04, 2008: Message edited by: Bert Bates ]
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole point of the name of a variable is that it is something useful for humans to read to understand your code at pre-compile time, and it's something the compiler can understand at compile time.

(If we ignore reflection and the meta-data that is maintained by the compiler) the name of a variable is lost at runtime. To try to do what you are asking is meaningless.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to use names that are created at runtime to keep track of values, look at the classes which implement the java.util.Map interface. A Map lets you associate a key - such as a String - with a reference to an object - which can refer to any object.

Bill
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Oliver Lee:
well,i am very appreciate all of you!Maybe i can only handle this in another way,because it is useful for me to to so. for example,i have a method,which need a var name as a parameter to be passed.


As others already mentioned... Given a reference and the name of the instance variable in that reference, it is possible to get the value.

Here is a method that given an instance, and the var name (as a parameter), it will get the value of that var name in that instance:



Henry
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have suggested the same from the very beginnig, to use Reflection API. Though it adds a little value, it seems that is the only option available. Right?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer this question:
Do you know at compile time all of the variable names that might be used in any classes you need access to by this mechanism?

If no, where do the names come from at runtime?

Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!