• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to convert String value into a New Variable name automatically  RSS feed

 
Abhi Bharti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,,


My question is suppose there is a String variable like String abc="Shini";

So is it possible to use 'Shini' as new variable name by some automatic means not by explicit typing.

String abc="Shini";

String Shini="somevale"; //By some automatic way not by hard coding.


Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, but perhaps a Map would suit your purpose.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Abhi Bharti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
variable names are only useful to someone reading the source code, so there is no real reason to do this.
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is this possible using reflection and horrible back doors? (half remembering a conversation heard between 2 very experienced java guys)
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really, no. You could create a new class which had new member variables using the names you wanted, but then the only way to access them would be also via reflection, so what's the point?

The desire to "create variables at runtime" is a very common request from new-ish programmers: that's the point at which they need to learn about arrays, maps, and other collections.
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:Not really, no. You could create a new class which had new member variables using the names you wanted, but then the only way to access them would be also via reflection, so what's the point?

The desire to "create variables at runtime" is a very common request from new-ish programmers: that's the point at which they need to learn about arrays, maps, and other collections.


oh yes in no way was i suggesting this was a sensible thing to try, just theoretically possible.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!