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 6086819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:47:58+00:00 2026-05-23T11:47:58+00:00

I have a class which holds some colors for a GUI, which the program

  • 0

I have a class which holds some colors for a GUI, which the program can change to its liking. For a specific element of the GUI, I’d like to be able to specify a color which is a member of that class. In C++, I could use something like

int Pallete::*color = &Pallete::highlight;
Pallete pallete; // made in or passed to the constructor
// ...
void draw() {
    drawing.color(pallete.*color);
    // ...
}

Is there an equivalent in java? I’ve thought about using getField(String) in the Class class, or keeping the colors in a Map with string keys, but neither of these seem like very good solutions, because they rely on strings, and the compiler can’t enforce that they are actually members of Pallete. I could also put all of the color names in an enum, and have some getter function which returns the associated color, but that seems like more work for me.

  • 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-05-23T11:47:58+00:00Added an answer on May 23, 2026 at 11:47 am

    You could just use the Integer class. Since its a class, it obeys reference semantics.

    This would give you a class like

    class Palette
    {
        public static Integer highlight; // this gets initialize to whatever value
    
        public Integer color = highlight;
    
    }
    

    Edit

    Okay, so the Integer class describes an immutable type, so you cannot change the value held by an Integer. The solution is pretty simple, though. You can just define your own, mutable class, like so:

    class MyInteger {
    
        private int value;
    
        public MyInteger(int value) {
            this.value = value;
        }
    
        public int getValue() {
            return i;
        }
    
        public void setValue(int value) {
            this.value = value;
        }
    }
    

    Of course, this example only provides the most basic functionality. You could always add more if you feel like it. You may also find it useful to use an internal Integer rather than an int, or perhaps also to extend the Number class (as the Integer class does).

    If you declare highlight and color to be of MyInteger type, and assign highlight to color, then changes to highlight will be reflected in color:

    Palette.highlight.setValue(0);
    palette.color = Palette.highlight;
    Palette.highlight.setValue(1);
    System.out.println(palette.color); // This line will now print "1" instead of "2"
    

    One potential drawback of this method is that you cannot write an assignment like

    Palette.highlight = 0;
    

    Instead you must use setValue() to change the value of an instance of MyInteger. However, I don’t think this is a great loss, since it achieves the functionality you requested.

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

Sidebar

Related Questions

I would like to create one class which holds some key values of the
I have a class which inherits QAbstractTableModel, and holds some complex structs in a
I have one base class which holds a map for function pointers like this
Suppose I have a LimitedValue class which holds a value, and is parameterized on
I have written an NHibernateSessionFactory class which holds a static Nhibernate ISessionFactory. This is
I have a class which is essentially just holds a bunch of constant definitions
I have a class which is marked with a custom attribute, like this: public
I have a class which looks something like this: public class Test { private
I have a parent class which holds a map and n the child class
Need help... I have 3 classes, Manager which holds 2 pointers. One to class

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.