3

I am creating instances of a class with extends Button and accessing an integer variable directly for better performance. I'm using constants to easily identify the variable's current setting.

I have the constants declared both the Button class and in the Activity class which is instantiating them. I have found similar questions and read it's not good practice to create a class just to hold constants.

What is the best way to use the same constant declaration in both classes?

I am a beginner programmer so it's quite possible I am overlooking a simple solution.

Button class:

public class GridButton extends Button {
  public int displayStatus;

  // constants for mine display status
  private static final int UNTOUCHED = 1;
  private static final int UNCOVERED = 2;
  private static final int FLAGGED = 3;
  private static final int HIT = 4;
  ...
}

Activity class:

public class PlayGameActivity extends Activity {      
  private GridButton[][] gridButtons;      

  // constants for mine display status
  private static final int UNTOUCHED = 1;
  private static final int UNCOVERED = 2;
  private static final int FLAGGED = 3;
  private static final int HIT = 4;
  ...

  // e.g. accessing displayStatus value
  if (gridButtons[currentRow][currentColumn].displayStatus == FLAGGED)
  {
  }
}
  • 2
    make this fileds public in `GridButton` class and then use em in `PlayGameActivity` like this: `GridButton.UNTOUCHED` – Selvin Apr 20 '12 at 11:49
  • any proof for this -> **I have found similar questions and read it's not good practice to create a class just to hold constants**? – waqaslam Apr 20 '12 at 11:50
  • @Waqas [stackoverflow1](http://stackoverflow.com/questions/66066/what-is-the-best-way-to-implement-constants-in-java) [stackoverflow2](http://stackoverflow.com/questions/3866190/java-constants-file) –  Apr 20 '12 at 12:06
  • @Selvin thank you that seems so obvious but it works! –  Apr 20 '12 at 12:06
  • there's no fixed rule for this. If its convenient for you to group them in one file, then do it. Dont worry about performance, it won't affect – waqaslam Apr 20 '12 at 12:09
  • I'm going with the Constants class method after all, performance probably isn't affected much as Waqas said. –  Apr 21 '12 at 10:35
  • A constants class will become a mess. It's good practice to keep constants with the class they relate to, if you have multiple classes using them just make them public.For example, Constant.SOMETHING is likely to become confused with Constant.OTHER_THING in no time at all. – nckbrz Oct 02 '16 at 19:02

3 Answers3

8

To share things, you could make the constants in a separate class and access it statically

class Constants {
  public static final int UNTOUCHED = 1;
}

And then in both classes you could go Constants.UNTOUCHED.

In this case, I'd avoid using magic numbers and replace them with an Enum.

enum DisplayStatus {
  Untouched, Uncovered, Flagged, Hit
}

And replace all of your int displayStatus with DisplayStatus displayStatus. Now it's clearly to you and other readers of the code what that int represents.

Ideally you always want to use a specific type to restrict the range of possible values. In your example, only the numbers 1-4 are valid, but your type is as wide as an int so could be any value (e.g. -1 or 23543).

Good luck!

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
  • Great response. I have been told to avoid enums because of a performance hit, however, it seems my lecturer might not be up to date - [stackoverflow](http://stackoverflow.com/questions/5143256/why-was-avoid-enums-where-you-only-need-ints-removed-from-androids-performanc) –  Apr 20 '12 at 12:19
3

What is the best way to use the same constant declaration in both classes?

Define them once (e.g., in PlayGameActivity) and use them from both places (by dropping the private modifier). GridButton can refer to PlayGameActivity.UNTOUCHED and so forth, if you get rid of private.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
2

I think that enum is a good solution as mentioned before. However since you have different states of the grid buttons you could consider using Java State design pattern. Since you are a beginner, as you say, you may find that a bit confusing and even unnecessary, but I believe that it will help you develop solid programming skills. Lastly I remember I had developed a minesweeper myself, in plain java though, not android, and I had used Observer pattern for the grid buttons. Once one button was changing, it notified the neighbour ones to change their state too.

Thomas Kaliakos
  • 3,274
  • 4
  • 25
  • 39