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 trial

Java Java Objects Meet Objects Creating Classes

Getting confused.

In the video there is:

PezDispensor dispensor=new PezDispensor();

now i want to know why we type the PezDispensor in front of dispensor. is it used to locate the PezDispensor. java file or it is used to use the class and also tell me the use of it there. Also the PezDispensor after new keyword tell me what that does there.

1 Answer

Adrian Orta
Adrian Orta
30,490 Points

You type PezDispensor the same way you type int.

int number; is the same as declaring: PezDispensor dispensor;

Youre declaring what type of object dispensor holds.

The "new" keyword is used to create a new instance of the PezDispensor class. Youre are stating that this is not just any other PezDispensor but a new one. You are creating it. Adding it to your program and inputing it to be stored in the dispensor variable name which holds objects of the PezDispensor type.

Moderator Edit: Moved response from Comment section to Answer section.

so please tell me what type of PezDispensor is.how pezdispenzor get an object.

Adrian Orta
Adrian Orta
30,490 Points

PezDispensor is the class that was created in the program. You create a class in order to make an entity that has variables and functions that you desire.


public class Human {
    private String name;

    public Human (String name) {
        this.name = name;

    public String getName(){
        return name;

Human is a class and now we need to create an instance of Human. You can imagine this like a world with a population of 0.

Human adam = new Human("Adam");

This would create an instance of the Human class bringing your population in your imaginary world to 1.

Thank you adrian for clearing my doubt.