• Post Reply Bookmark Topic Watch Topic
  • New Topic

Returning array name  RSS feed

 
Miley Johnson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 

is it possible to get the names of the variables in array colors through code?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You can not refer to variable names in Java. If you want a name associated with a color, you can either create your own Color enum, or build a dictionary.
 
Michael D Sims
Ranch Hand
Posts: 113
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You COULD do something like this:

(although I realize this is probably not the ideal way to go about it ... it could work depending on how you need to implement things)



Which produces this output for me:
>
 
Michael D Sims
Ranch Hand
Posts: 113
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This also works:

 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use parallel arrays like that. Create a MyColour class with Color and String fields. Overriding the toString method shou‍ld allow you to display the name.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use an enum, as Stephan suggested. See Enum Types in Oracle's Java Tutorials to learn about enums.
 
Michael D Sims
Ranch Hand
Posts: 113
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't use parallel arrays like that. Create a MyColour class with Color and String fields. Overriding the toString method shou‍ld allow you to display the name.

I tried this, Campbell, and I could not get the Array that I created with the custom class to actually return a Color object that could be used. I was able to get the text name that I assigned to the color, but not the color itself as a usable object.

Perhaps a simple example for clarity?



Mike Sims
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Import statements omitted.
 
Michael D Sims
Ranch Hand
Posts: 113
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:[code=java]Import statements omitted.

Nice,

Why is it not necessary for the class to extend Color?

Mike
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael D Sims wrote:. . .Nice,
Thank you
Why is it not necessary for the class to extend Color?

Mike
“Favour composition over inheritance.” Search for that quote. That will explain it.

You probably could create a class called NamedColour which extends Color. You will then have difficulties with the equals() method. If you have x=NamedColour("red", Color.RED) and y=NamedColour("green", Color.RED) and z=Color.RED, which is going to show equal?
x.equals(z) will be true, and y.equals(z) will be true, and x.equals(y) will be false. So you are now in a position where you can't maintain the general contract for equals. And are they going to have the same hash codes? You will have two objects returning true from ob1.equals(ob2) with different hash codes, which is also wrong. If you have my MyColour class, all those comparisons will return false. So you are all right. You can have a sameColourAs(Color c) method as will if you wish.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!