Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7855099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:16:51+00:00 2026-06-02T20:16:51+00:00

I am creating instances of a class with extends Button and accessing an integer

  • 0

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)
  {
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:16:52+00:00Added an answer on June 2, 2026 at 8:16 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´m dynamically creating an instance of a class with reflection and this works fine,
I'm trying to register a factory method for creating instances of an open generic
I'm creating a bash script to create new EC2 instances and then upload a
I am creating a custom ASP.NET AJAX server control in which multiple instances of
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I'm using the Ninject Factory Extension and creating a custom instance provider explained in
I have a class which extends Application in an Android tabHost app. In the
I'm creating a soundboard. When I click on a button, the soundfile is played
I'm creating an XML-RPC server in Java using the Redstone XML-RPC library . The
I have this line of code. class ButtonPanel extends JPanel implements ActionListener { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.