Sharing constants in multiple classes (android minesweeper)

I instantiate the class with the Button extension and access the integer variable directly for better performance. I use constants to easily define the current setting variable.

I have constants declared by both the Button class and the Activity class that creates them. I found similar questions and I don’t think it’s good practice to create a class to just hold constants.

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

I am a beginner programmer, so it is possible that I missed 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;
  ...
}

Action 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)
  {
  }
}
+3
source share
3 answers

,

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

Constants.UNTOUCHED.

Enum.

enum DisplayStatus {
  Untouched, Uncovered, Flagged, Hit
}

int displayStatus DisplayStatus displayStatus. , int.

, . 1-4, int, (, -1 23543).

!

+8

?

(, PlayGameActivity) ( private). GridButton PlayGameActivity.UNTOUCHED .., private.

+3

, , . , , Java . , , , , , . , , , java, android, . , , .

+2

All Articles