Is it possible to write a static method that can alter all instances of an object of that class

Viewed 244

I'm trying to set the background color of all instances of a PanelGen class through a static method so that another class can change the color of all Panels at once.

An example of this would be:

 PanelGen a = new PanelGen(Color.red);
 PanelGen b = new PanelGen(Color.blue);

Then in the PanelGen class: public static void changeColor(Color newColor) { //Do something that changes the color of both a and b }

This way, an outside class such as "GameMaster" can change the color of both a and b

PanelGen.changeColor(Color.Orange);
3 Answers

One way to do this would be to have a static list in your PanelGen class. And then in the constructor, add that value to the list. Something like this:

    public class PanelGen{
        private static List<PanelGen> panelGenList = new ArrayList<>();
        private Color color;
        public PanelGen(Color c){
            color = c;
            PanelGen.panelGenList.add(this);

        }

        public void setColor(Color c){
            color = c;
        }

        public Color getColor(){
            return color;
        }

        public static void changeColor(Color c){
            for(PanelGen t: panelGenList)
                t.setcolor(c);
        }

        public static void main(String [] args){
            PanelGen a = new PanelGen(Color.red);
            PanelGen b = new PanelGen(Color.blue);
            System.out.println(a.getColor());
            System.out.println(b.getColor());
            PanelGen.changeColor(Color.Orange);
            System.out.println(a.getColor());
            System.out.println(b.getColor());
        }


    }

If all panels must have the same colour, then have one instance of colour, rather than trying to sync N different instances.

  class PanelGen {
     static Color colour;
     static void changeColour(Color c)  { colour = c }
     Color getColour() { return colour; }
         :
     }

The getColour method could be declared static but I assume you might want to use it as if it was an instance member.

(Yes, the color/colour thing was intentional :-))

The Answer by Justin is basically correct and would work. I have two concerns.

Memory leak

One concern is a memory leak, assuming the panels can be discarded before the app terminates, as I commented.

Use weak references in the list of panels, to avoid the memory leak.

If panels always survive until the end of the app execution, then there is no problem. You can ignore this point.

Separation of concerns

The other concern is separation of concerns. If the panels are designed to have separate colors, but some other entity such as GameMaster wants to control the color of all the panels, then it is the GameMaster class that should contain the collection of panel instances, rather than a static list on the panel class.

How then to register each new panel with the GameMaster? If you have only one place where you instantiate, pass that entity a reference to GameMaster to access its list and add the new panel. Most likely, it is GameMaster itself that is that place that is instantiating the panels, and can there register each new instance with its list of panels.

By the way, your name PanelGen bothers me. If the “gen” refers to “generator” then you may be conflating a panel manager/generator with the panels. There should be two classes, in this case, not one combo. This is a vital point in this context, as we discussed above where to go assign responsibility for instantiating panels, then tracking their existence, and changing uniformly their color.

Related