How to use a static method to increase a non-static variable?

I have a Java class called Game that has a non-static integer called evaluation.

I would like to implement a static method that would increase the score of each game object by 1, with the name IncreaseAllScore (). Is it possible? Can I imitate something like this or is there any way around this?

+4
source share
7 answers

You can do this with an implementation as follows:

int score; static int scoremodifier; public static void increaseAllScore() { scoremodifier++; } public int getScore() { return score + Game.scoremodifier; } 
+5
source

The only way to do this is to provide a mechanism for a static method of accessing a link to a game object. One way to do this is to have each Game object register in a static data structure.

For example, you can do this:

 public class Game { private static Set<WeakReference<Game>> registeredGames = new HashSet<WeakReference<Game>>(); private int score; public Game() { // construct the game registeredGames.add(new WeakReference(this)); } . . . public static incrementAllScores() { for (WeakReference<Game> gameRef : registeredGames) { Game game = gameRef.get(); if (game != null) { game.score++; } } } } 

I use WeakReference<Game> here so that the set does not interfere with the game collecting garbage when there are no other links to it.

+2
source

This is technically possible, but usually it is a bad design * . Instead, create a container for all of your games (name it class Games ), which will contain links to all created instances of the Game . Most likely, the Games class will have a createGame() method to fully control the life cycle of all created games.

Once you have the Games class, it can have a non-static increaseAllScores() method, which will basically iterate over all created instances of the Game and increase the number of all of them one at a time.

* - create a static List<Game> all instances and change this list inside the Game constructor.

+1
source

This will be the outline of the solution to your problem:

 static final List<Game> games = new ArrayList<>(); public class Game { public Game() { games.add(this); } } public static void increaseAllScore() { for (Game g : games) game.increaseScore(); } 
0
source

This is possible, but requires some bookkeeping. Essentially, you need to keep a static set of pointers to all existing games. In the game’s constructor, you need to add it to this list, and you need to delete it again in the destructor.

Probably the best way would be to have a static variable called scoreOffset or similar. You can then calculate the score of the game by taking the score of the instance and adding a static scoreOffset .

0
source

You can only do this if your increaseAllScore method has static access to Game instances (you can pass a list in arguments or have a statically saved list).

0
source

It's impossible; learn the basics of object-oriented programming first.

As a workaround, you can find a link to all games:

 public class Game { private static List<Game> allGames = new ArrayList<Game>(); public Game createNewGame() { Game game = new Game(); allGames.add(game); return game; } public static void increaseAllGames() { for (Game game : games) { game.increaseScore(); } } } 

This is just an implementation example; for design, I would not place them in one class.

0
source

Source: https://habr.com/ru/post/1445870/


All Articles