Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKhaokong Nopwattanapong
3,578 PointsPezDispenser dispenser = new PezDispenser(); What does () means??
So what is the use of () without nothing inside it?
1 Answer
Jason Thomas
12,519 Pointsit 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!