Reputation: 25
So, the question is. If I'm calling method guess from class - Player
and it is a void-type method without return statement in it, how come I'm able to store result of number = (int)(Math.random() * 10)
in number
variable for 3 different objects (p1, p2, p3)
?
I'm little confused about when should I use return statement or void-type methods, because if number = (int)(Math.random() * 10)
is giving some results which I want to use, why then I don't need to return this results from a method to pass them to the number
variable which I declared in int number = 0;
public class Player {
int number = 0;
public void guess() {
number = (int)(Math.random() * 10);
System.out.println("I'm guessing " + number);
}
}
Upvotes: 2
Views: 2328
Reputation: 12819
A void
method does not return anything, but it still allows you to do things. (Print to the console, modify variables etc) The void
keyword just means that it doesn't return a value. (In void
methods you can still use a blank return;
to end the method) And because you are modifying your number
variable in the GuessGame
object the changes you make will stay even though you don't return a variable. Try this simple test to see what I mean:
//In your GuessGame class
int number = 0;
public void foo() {
number++;
}
public static void main(String[] args) {
GuessGames games = new GuessGames();
games.foo();
System.out.println(games.number);
//Outputs 1
}
docs for the return
statement
Upvotes: 3
Reputation: 20875
The point is: where is the result of Math.random() * 10
physically stored on your computer when your program is run? You list two options.
Options 1: Instance field
In this case the compiler instructs your operating system to reserve space for a int
variable for the whole life of the Player
object. The player object may live for microseconds, seconds, minutes, hours, days, months, ... it depends! This storage space is usually find in the RAM of the computer and from Java you can access it with the syntax myPlayer.number
as long as you have a Player reference somewhere.
Options 2: Return value
In this case the compiler finds the space to store the result of the computation in a register of the Java virtual machine, that you can mentally map to a register of the physical processor. This value will only at best survive for a couple of processor cycles (there are gazillinos in a GHz CPU, so it's really a tiny little fracion of a second) if you don't store it somewhere else - and if you don't it's lost forever. See the following example:
private int someRandom;
private int gimmeARandom() {
return Math.random() * 10;
}
private int test() {
int someRandom = gimmeARandom(); // --> store the value until end of method
this.someRandom = someRandom; // --> further keep it so we can read it later
gimmeARandom(); // --> what did it returned? We'll never know
}
Upvotes: 1
Reputation: 809
Void is different than static - void
just means the function does not return anything, but it can still be a instance method, i.e. one that is associated with each new
instance of a class. I think you're confusing this with the functionality of static
, which allows methods to be called without an instance of the class.
Upvotes: 0