This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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!
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?
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
Joined: Nov 02, 2012
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.