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 Constructors

PezDispenser

What do you mean @1:08 he's returning a PezDispenser ?? It's starting to get overwhelming?

4 Answers

Jacek Szemplinski
Jacek Szemplinski
6,423 Points

It means that this function:

public PezDispenser(String characterName){
    mCharacterName = characterName;
  }

will return object of type PezDispenser. This specific type of function is called constructor function, you can read more about it here https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

Thank you Jacek, i will read more.

Hi Jonathan,

Don't worry too much about getting overwhelmed - if you haven't learned these concepts before, then they should be a bit overwhelming! If you keep going through the lessons, often the concepts will become clear as you go - but if not, looking for help or clarification is definitely a good option.

"Returning a PezDispenser" means that the method is going to create an Object, an instantiation of your class. You could create a default PezDispenser without a constructor, which would just be new PezDispenser as the class is described - which means, no required arguments (and Yoda as the mCharacterName - until that line is deleted, later in the video). However, making the constructor method is giving a second option, which is that you could instead make a new PezDispenser("Chewbacca") which would have the mCharacterName be Chewbacca. In Java, you can have multiple identically named methods as long as they take different arguments, and you specify which method to use by including the appropriate argument when the method is called.

Hope that helps, let me know if you have any questions about the explanation or constructors!

Thank you Evan, I wouldn't say I understood everything in your answer but it surely helps and yes I am completely new to all this..so at times things just don't make any sense. I will take your suggestion and will keep going through the lessons and hopefully it should help as mentioned by you, my biggest challenge is the language- when you say "take different arguments"what does that mean in a more simpler language? You know what i mean...

Thank you again so much, and maybe i will bother you in the future again.

In general, an argument is whatever a function is taking as an input. So in this example, the argument is the string you provide to create the character with; so "Yoda" or "Chewbacca".

This can be confusing, because the parameter is what you call the request for the argument.

For example, if you define the add function as:

int add(int x, int y) {return x + y;}

Then x, y are parameters, while if this is called as add(2, 3), then 2, 3 are the arguments. Note that variables from the calling context can be arguments: if the subroutine is called as a = 2; b = 3; add(a, b) then the variables a, b are the arguments, not only the values 2, 3.

The language used is definitely a challenge unto itself. Procedure, function, subroutine, subprogram, and method are all different words you'll hear used that actually mean the exact same thing; sometimes coders will separate these words out, but sometimes not!

I sincerely appreciate your efforts Evan....this is a bit more clear.