wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes enums and files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "enums and files" Watch "enums and files" New topic
Author

enums and files

Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

Hi Guys,

Question regarding enums and there use.
I created a checkers/draughts game for my final year project in college years ago and I am going over it again to update it.
I had various integer variables in the original program that I used to hold information such as what piece was on a square Who�s go is it etc.
Should I create a separate enum file for each new enum I need to create?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I had various integer variables in the original program that I used to hold information such as what piece was on a square Who�s go is it etc.

Not sure how this maps to an emun. Did you have constants numbering squares from (say) bottom left to top right or something?

What is the scope of your constant values? Do you use these Integer variables outside the cope of a single class? If not, why change them?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

Thanks for the reply.

I had a variable for say whos go it was.
1 would be the computer 2 would be the human.
This was ok but when reviewing code it sometimes would of been easier to view
computer rather than 1 in the code.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So, I imagine you have a bit of code that does this:

or something or other. If so this will look almost identical if you swap it to an enum. There is no harm in doing this, except that enums can be null, ints can't, so you have an extra case to cater for. If your constant is an Integer you already have this problem though.

My point about the scope of the constant valiable is that if you do expose this to other classes then yes, an enum is probably better. The old constant pattern was not type-safe, enums are. If your constant variables are private to the class that uses them, then you are not really gaining much by changing them. You are not really losing much either, so its up to you really. Whatever you feel makes more sense in the code.
[ August 19, 2008: Message edited by: Paul Sturrock ]
Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

Thanks for that. I guess there is no wrong or write answer to the question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enums and files