Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I return the name of a boolean variable and not the boolean value?

 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question says it all. I am writing a method in which I need it to return the actual name of a boolean variable and not just the true or false value assigned to that variable. I thought toString might work, but that will just print "true" or "false" so it won't work. I'm sure this is a simple thing, but I have searched and can't find a good answer. Here's what I have come up with but it seems like it won't work since the equals operator will just tell me if spot has the same boolean value as the variable "one", which is the name of the boolean variable I am trying to return. Thanks for any help!
 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting question. I searched a bit as well and came up with this:



Here's the source http://stackoverflow.com/questions/744226/java-reflection-how-to-get-the-name-of-a-variable.

Do note that this probably won't work with local variables.



 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you're a beginner, there's a chance that you are trying to do that (which as you have found, can't be done) because it was the answer to some other question you had. And unfortunately you're probably going down the wrong track. In Java there is almost never any need to pass around the names of variables. So what was it which prompted you to want to do that?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a better question is "why on earth would you want or need to do this?"

variable names are a convenience for a programmer. They really mean almost nothing to a machine.

I'm not sure...it might be possible using reflection, but since I've never used it for anything, I couldn't say for sure.

I am 99.999% sure that if you think you need the name of a variable, you are thinking about the problem all wrong.
 
james falk
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right. I thought about it, and I can accomplish what I need to do with Strings instead. I am building a tic tac toe game, and we have to program a low level of artificial intelligence into the game, and I was using some boolean methods to make 'educated' moves based on certain truth table patterns, but the decisions can still be made by the boolean methods without me needing to pass a name of the boolean variable out of the method. Fun little project though. Thanks for the insights guys.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fun projects are the best. Carry on then, and if you run into other roadblocks you know where to find us.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic