'Updating Java object attribute incorrectly?

EDIT: Please see edit towards bottom

I have the following class:

public class Player { 
        SingleGameRound roundOne = new SingleGameRound(); 
        float score = 0.0; 
         
        <constructor> 
         
        public String toString() { 
                float roundScore = this.roundOne.getScore(); 
                this.score = roundScore; 
                return "";
        }
        
        public float getResult() {
                return this.score;
        }            
}

I'm trying to update this class's score attribute with the value returned by getScore() (a method belonging to a different public class singleGameRound).

However, after instantiating a Player object and calling the getResult() method on it, the default value of score is returned instead of an updated value. It seems like score never gets updated.

I tried using a setter method to update the attribute but the same thing happened too. Am I updating the attribute incorrectly? TIA!

(note: my actual code doesn't look like this exactly but generally follows this structure)


EDIT 1) After going back to the code for the SingleGameRound class, I decided to make some changes.

Before, this class would also utilise an adapted toString() method to calculate the score of a single game round - however, this score wasn't stored anywhere (it would just be returned in a string).

I've added an attribute to the SingleGameRound class so that it can store the calculated score value. My code for that class now looks like this:

public class SingleGameRound { 
        private float roundScore = 0.0f; 
         
        <constructor> 
         
        public String toString() { 
                float score = *some calculations*
                this.roundScore = score;
                String resultLine = "You scored " + this.roundScore;
                return resultLine;
        }
        
        public float getScore() {
                return this.roundScore;
        }            
}

Now, if I create an object of SingleGameRound and do System.out.println(SingleGameRoundObj), I get e.g. >>> You scored 10.5

But if I do System.out.println(SingleGameRoundObj.getScore()), I just get >>> 0.0. My original problem still exists, the values of my attributes aren't updating in either class.

(Apologies for all the faff and many thanks if you made it this far, sincerely)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source