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 (Retired) Meet Objects Privacy and Methods

Guy Hagan
Guy Hagan
1,985 Points

I do not understand what this line of code does

PezDispenser dispenser= new PezDispenser(); What does this line of code actually do ? .what does new do? .What does PezDispenser(): mean? .Why do you have to put PezDispenser before defining your variable?

Thanks All answers appreciated

Edit: Thanks very much for all your answers i know understand this code a lot better

3 Answers

jacksonpranica
jacksonpranica
17,610 Points

EDIT: Accidentally posted as comment to the question rather than answer.

This is coming from someone who just started Java and Android about 2 months ago with no experience at all in coding before. So this may not be the most thorough or professional answer as you will later get. I encourage you to wait for those and read them, but for the time being!!

Do you remember when you initialize a new string variable? You throw out a:

String name = "Ben";

In here, String is the type of data you are coding, name is the variable that the String data is put under, and "Ben" is the actual data.


This is the same kind of structure when initializing a PezDispenser:

PezDispenser dispenser = new PezDispenser();

So let me break it down for you. While I can't really remember that exact lesson fully, I do believe in some other file you have created a model for what a PezDispenser object is. The kind of data it holds, the kind of methods it has, etc. This defines PezDispenser. It should be a public class file called PezDispenser.

Just like with a String the PezDispenser needs to be initialized in the main java process so that there is a PezDispenser to work with in the code. Just like you couldn't use the String name unless you initialize it as show above.

So PezDispenser is the Type of Data you would code, dispenser is the variable name and then we move onto the hard part, new PezDispenser().

So let me tell you something. You can actually make a new String like this:

String name = new String();

In this case the string will be empty and will not hold any data e.g. "". new String() is just saying create an empty string.

So "new PezDispenser()" is basically creating an empty PezDispenser object that you can then use methods like "refill" or "Take a pez out" in order to add or remove data from that PezDispenser respectively.

Its a really odd concept to wrap your mind around, but once you get it, it will make tons of sense.

PezDispenser dispenser = new PezDispenser();

creates new object from PezDispenser class. Key word new indicates creation of new object.

Breaking it down to smaller slices:

PezDispenser dispenser

creates dispenser object from PezDispenser class. PezDispenser is used just as e.g String followed by variable name.

= new PezDispenser();

Assigns brand new PezDispenser class object to dispenser. PezDispenser() indicates usage of non-parameter constructor (available by default).

Hello Guy.

when you type this line : PezDispenser dispenser= new PezDispenser()

you actually making a new object in example program.

in PezDispenser class you give the object fields like name, age, etc

and then you create the actually object in main the main program when you type PezDispenser dispenser= new PezDispenser()

Hope it make sense to you!

tell me if it does or not :)