This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How do I return the name of a boolean variable and not the boolean value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I return the name of a boolean variable and not the boolean value?" Watch "How do I return the name of a boolean variable and not the boolean value?" New topic
Author

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

james falk
Ranch Hand

Joined: Nov 02, 2012
Posts: 55
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

Joined: Feb 10, 2013
Posts: 117

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Oct 02, 2003
Posts: 11354
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
james falk
Ranch Hand

Joined: Nov 02, 2012
Posts: 55
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Fun projects are the best. Carry on then, and if you run into other roadblocks you know where to find us.
 
GeeCON Prague 2014
 
subject: How do I return the name of a boolean variable and not the boolean value?