How to reset a variable in java

Is tright here a method to reset a value of a static variable to their initial state? For example:

I have actually a lot of variables which holds score, speed, and so on All those variables are changing during the routine execution. So as soon as the user stops working in a game, I would certainly favor to recollection ALL variables to their initial state. Is tright here some way? Since i don"t understand if it is a great idea to do this manually for EACH variable in my regimen. for example:

static int SCORE = 0;static float SPEED = 2.3f;public void resetGame() SCORE = 0;SPEED = 2.3;
java
Share
Improve this question
Follow
asked May 8 "14 at 15:17

*

rootpantherarootpanthera
2,36766 gold badges2626 silver badges5555 bronze badges
5
Add a comment |

8 Answers 8


Active Oldest Votes
14
Use a things, and also collection its initial state in the constructor:

public class GameSettings personal int score = 0; exclusive float speed = 2.3F; // methods omitted for brevity...public void resetGame() gameSettings = new GameSettings();Also, please respect the Java naming conventions. ALL_CAPS is reserved for constants. Variables should be lowerCase.

You watching: How to reset a variable in java


Share
Improve this answer
Follow
answered May 8 "14 at 15:22
*

JB NizetJB Nizet
629k7979 gold badges10991099 silver badges11691169 bronze badges
1
Add a comment |
3
Store the default worths.

static final int DEFAULT_SCORE = 0;static last float DEFAULT_SPEED =2.3;static int SCORE = DEFAULT_SCORE;static float SPEED = DEFAULT_SPEED;public static void resetGame() SCORE = DEFAULT_SCORE; SPEED = DEFAULT_SPEED;
Share
Improve this answer
Follow
answered May 8 "14 at 15:19

*

Anubian NoobAnubian Noob
12.8k55 gold badges4747 silver badges6969 bronze badges
4
Add a comment |
2
Why not just recreate the object if you desire it reset? Then it"ll implicitly have actually the default values.


Share
Improve this answer
Follow
answered May 8 "14 at 15:21
*

Evan KnowlesEvan Knowles
7,00622 gold badges3232 silver badges6666 bronze badges
1
Add a comment |
1
You could simply declare your variables without worths and also have a technique initGamestate() which sets all variables to their initial values. Call this feature both on initialization of the application and also as soon as the user starts a new game.

An even more object-oriented solution would certainly be to have a class GameState which has actually all these variables and sets the default in its constructor. You then start eextremely game by initializing a fresh object with brand-new GameState();


Share
Improve this answer
Follow
answered May 8 "14 at 15:21
*

PhilippPhilipp
60.1k99 gold badges107107 silver badges141141 bronze badges
2
Add a comment |
0
You"ll just have to recollection them one by one. If you"re worried around typos you could do: int initialscore = 0; int score = initialscore; and then reset them to the initial... variables in your function.


Share
Improve this answer
Follow
answered May 8 "14 at 15:21
porglezompporglezomp
1,2081212 silver badges2222 bronze badges
Add a comment |
0
I have many variables which holds score, rate, etc

You must put them all right into one class and also eextremely member acquire initialsed (if the default will not work).You will hold the player"s state in one reference to an object of this course. To recollection simply develop a brand-new object of this class and asauthorize it to the referral.


Share
Improve this answer
Follow
answered May 8 "14 at 15:23
PeterMmmPeterMmm
22.1k1111 gold badges6767 silver badges103103 bronze badges
Add a comment |
0
Usually games usage more than one thcheck out (for instance as soon as making use of Swing or painting any kind of graphics), e.g. so inputs are not blocked. That means through all the various other services you could run right into race conditions. This solution of Evan Knowles came closest.

See more: My Google Drive Full But No Files In It Issue, Troubleshooting Google Drive Storage Full Error

I would suggest a immutable GameState class, e.g.

public GameState private last int score; exclusive final int speed; // initial state/"recollection game" public GameState() score = 0; rate = 2.3; // Private so we are constantly in a valid state private GameState(int _score, int _speed) score = _score; rate = _speed; public GameState updateSpeed(int _speed) return brand-new GameState(this.score, _speed); public GameState updateScore(int _score) rerevolve brand-new GameState(_score, this.speed); public int getSpeed() rerotate speed; public int getScore() rerotate score; // add getters, setters and whatsoever right here. But watch for proper synchronization between threads!The benefit here is that when the values are assigned, they can"t be adjusted anymore ("immutable"/read-only) from the outside, so there are no concurrency concerns. Plus, you acquire a sort of chaing for free (see below)! Furthermore, you can safely (de-)serialize the game state for saving/loading games.

Effectively, each GameState represents one state in a finite state machine. Calling either updateSpeed or updateScore is a change to a new state.The public default constructor is a change to the initial state of the state machine.On a side note, the state machine is finite bereason the value ranges of score and rate are finite, therefore all combiniations of them cause a finite amount of states.

I currently assume your course for doing other game stuff is dubbed Video Game.

See more: Nvidia Graphics Adapter Wddm1 1 Download, Windows Update Nvidia Graphics Adapter Wddm1

public Game private volatile GameState gameState = brand-new GameState(); public void resetGame() gameState = new GameState(); // Just an instance public increaseSpeed(int _additionalSpeed) gameState = gameState.updateSpeed(gameState.getSpeed() + _additionalSpeed); // Change even more than one value public changeBoth(int _newSpeed, int _newScore) // First way: Create a new GameState, adjust both values action by action and also then asauthorize thereafter. GameState _newState = gameState.updateScore(_newScore); // various other computations follow // Do NOT asauthorize to gameSpate right here, because the state would certainly be inconsistent then. _newState = _newState.updateSpeed(_newSpeed); // At the END of the technique, assign the brand-new game state. That ensures that the state is constantly valid gameState = _newState; // 2nd way: Use strategy chaining if you don"t have to make other computations in in between. Aacquire, do this at the finish of the strategy gameState = gameState.updateScore(_newScore).updateSpeed(_newSpeed); The volatile keyword renders certain eexceptionally threview sees the exact same worth of the gameState variable. You can desire to think about utilizing other synchronization/locking methods rather, also. Additionally you can make the gameState field static and skip the volatile keyword if you only have actually one Video Game object.Since GameState is immutable(read-only), the state of your game now is always consistent/valid.