• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help Using Enum Appropriately  RSS feed

 
Justin Coombs
Ranch Hand
Posts: 31
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a little bit unsure whether I have the right idea with enums, so I'll post a bit of sample code and ask for your feedback about whether this is a proper usage of enums.
Say I have a class "Paddle" in a Pong game and I want to have 3 enums to model the direction in which a paddle is moving - NORTH, SOUTH, STATIONARY. Would the following code make sense? If not, please try to explain how I'm going about it the wrong way



And the enum class...


As you can see, I call updateYVel() in the methods setYSpeed() and setDirection() because a change in speed and a change in direction would both affect the velocity. The yVel is multiplied by either 1 or -1 in order to be used in the coordinate system of a java GUI, and naturally a stationary object should have it's speed and yVel set to 0. Does this make sense or should I be going about it a different way?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes sense, I see nothing wrong with your code.
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should have the ±1/0 as fields of the enum constants. You can then say something like
this.speed = speed * direction.getConversion();

That way the object is taking care of itself. The ±1/0 is now part of the direction not the paddle.
 
Justin Coombs
Ranch Hand
Posts: 31
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies.

So I took your advice Campbell Ritchie and tried to write out the code for the enum, but I'm confused about the syntax for enum classes. Here's what I have



And then I needed to adjust the Paddle code by initializing variables and changing updateYVel()...







What confuses me most is the parameters for an enum, because the presence of a constructor leads me to believe that I should be instantiating them, and in this case I should be passing it an int. But after looking up more on enums, I discovered they are always implicitly static and cannot be instantiated as a new object. So mostly its the constructor that throws me off because in my mind I've always associated constructors with instantiating new objects. Also, the syntax of using the parentheses beside the enum constants, such as NORTH(1) is confusing. It seems as though the parameter list of the constructor must match the parameters in the parens beside the enum constants themselves in both quantity and type, which is understandable, but it's so weird because in client code, there is no parameter passing - in class Paddle I simply declare direction = Direction.STATIONARY!. Simple constants are so much easier to understand. But now that the changes have been made, I can see the benefit...the switch statement has been completely eliminated from the whole project and yet it all works exactly the same!

Nevertheless, even after this big post, if there's a more proper way I should be doing the enum class, I'd appreciate further correction
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Coombs wrote:I discovered they are always implicitly static

I think what that article was saying is that each element of the enum class is a static reference to an instance of the enum class.
If it helps, try thinking of the Direction class as a class that holds a number of instances of itself - one for each element. In your case it will hold 3 instances and STATIONARY, NORTH and SOUTH are static references to these instances.
So the direction class would actually look something like this

Justin Coombs wrote:cannot be instantiated as a new object

They cannot be instantiated in your code, but a single instance of each member of the Enum is instantiated when the Enum class is loaded.
The only way to access them is as you would access any other static variable - by dereferencing the class name.

 
Justin Coombs
Ranch Hand
Posts: 31
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joanne. After seeing your code snippet with the more friendly syntax, and reading a bunch of enum examples, especially oracles own example at the docs I have a much better comprehension.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!