SAF
SAF

Reputation: 121

Setting the value of a variable from another class in Java

I was wondering if it is possible to reset the value of a variable from another class. For example I have this variable in a HillClimber (hc) class:

    public int ballWeight = 200;

What I want to do is run a simulation of a game with the ball weighting at this value. When it is finished I want to set the value to 201 from another class and begin the simulation again, and after that increase to 202 and start another and so on. My problem is that every time I restart the simulation the ballWeight variable is reset to 200. I have tried using a setter method in the HillClimber class:

    public int setBallWeight(int ballWeight) {
        return this.ballWeight = ballWeight;
    }

and called it from another class at the end of a simulation:

    hc.setBallWeight(hc.ballWeight+1);

but this does not seem to work as the variables stored value is not changed. Does anyone know how I can do this so the stored value of ballWeight will be increased by 1 each time a simulation ends? Or is this even possible? Thanks.

Upvotes: 0

Views: 19345

Answers (1)

Narmer
Narmer

Reputation: 1454

Usually in a POJO you have what are called a getter and a setter method for every variable of the object. In your case:

public class HillClimber{
    private int ballWeight;

    public HillClimber(){
        this.ballWeight = 200;
    }

    public void setBallWeight(int ballWeight){
        this.ballWeight = ballWeight;
    }

    public int getBallWeight(){
        return this.ballWeight;
    }
}

In this way you can access the variable ballWeight via get and set method. You don't access it directly like in hc.ballWeight, which is possible but is a bad practice, and prevent this access type declaring your variable as private (meaning that only the class in which it is declared can directly access it).

To fullfill your request of adding one at every run of the game you can therefore call

hc.setBallWeight(++hc.getBallWeight()); //Equivalent to hc.setBallWeight(hc.getBallWeight() + 1);

I usually don't use this approach if the class isn't automatically generated (as in an Hibernate context), but instead declare another method in the HillClimber class

public void incrementBallWeight(int ballWeightToAdd){
    this.ballWeight += ballweiGhtToAdd; //Equivalent to this.ballWeight = this.ballWeight + ballweiGhtToAdd;
}

or if I always need to add only one to my variable

public void incrementBallWeight(){
    this.ballWeight++;
}

and then simply call incrementBallWeight after every game run.

NB: to have this working you will have to use always the same instance of HillClimber. In your main

public class Game{
    private HillClimber hc = new HillClimber(); //Create the instance and sets ballWeight to 200
    public static void main(String[] args){
         playGame();
         hc.incrementBallWeight(); //ballWeight == 201

         playAnotherGame()
         hc.incrementBallWeight(); //ballWeight == 202 -> Always the same instance of HillClimber (hc)
         .
         .
         . 

    }
}

EDIT

I think your problem is greater than that. You are asking to save the state of a variable ( meaning that this value should be available also if you turn off and on your pc) without using a permanent storage. This is simply unachievable.

You should rethink your program (and I mean java program, not a "game run") to not stop after every game run. You can do this in different ways: via Swing GUI, via user input from stdin and so on. If you want some help on this topic, we need to know more of your code (maybe putting the whole of it is best).

OR you can use a file to store your value, which is not as difficult as you think. (Also).

Upvotes: 3

Related Questions