• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 117
Mac Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic