Java Java Objects Meet Objects Creating Classes

Khaokong Nopwattanapong
Khaokong Nopwattanapong
3,320 Points

PezDispenser dispenser = new PezDispenser(); What does () means??

So what is the use of () without nothing inside it?

1 Answer

Jason Thomas
Jason Thomas
11,736 Points

it means that it's a method (or in this case a constructor) that can be used without any arguments.

some you want to require an argument, some you don't.

let's say you had a Car object that kept track of speed as an integer. you might have a method for increasing the speed by however much the driver wants. in this case you might want to pass in a value from the user in which case it might look like:

public void speed (int speedChange){
    carSpeed += speedChange;

but you could also have a method that just always sped up the car by one increment that didn't require an argument:

public void speedUpOne (){
    carSpeed += 1;

this second method just does the same thing always and it's not required to have anything passed in for the method to do its job. In the case of the constructor you listed above, it might have built-in default values that are used if no arguments are entered when a new object is created.

hope this helps!