Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Lucas Santos
Lucas Santos
19,315 Points

Kind of confused with constructor parameter and method parameter.

So like in the example we made a simple class with a name field

public class PezDispenser{
private String mCharacterName;

//constructor
public PezDispenser(String name){
mCharactername = name;
}

//method
public String getCharactername(){
return mCharacterName;
}
}

My question here is why did we give an argument to the constructor with the name? wouldn't we be able to do that in a method like we did for calculating PezCount ?

Let me put it this way, what if I wanted to create a mLastName field and a mGender field. Would I have to set arguments in the constructor instead of making a method?

Like would I have to do:

public class PezDispenser{
private String mCharacterName;
private String mLastName;
private String mGender;

//constructor
public PezDispenser(String name, String lastName, String gender){
mCharactername = name;
mLastName = lastName;
mGender = gender;
}

//method for getting name
public String getCharactername(){
return mCharacterName;
}

//method for getting last name
public String getCharacterLastName(){
return mLastName;
}

//method for getting gender
public String getGender(){
return mGender;
}
}

and if so that means I would need to make a new name, lastname and gender when creating a new instance because they are not all the same correct?

Example:

PezDispenser dispenser = new PezDispenser("Lucas", "Santos", "Male");

Can someone please elaborate a bit on when to give arguments to a contractor and a method, thats the parts thats confusing me because in the example we did not return the PezAmount like we did the mCharacterName

Thank you!

3 Answers

Andrew Mitchell
Andrew Mitchell
2,739 Points

Because instead of calling PezDispenser pezDispenser = new PezDispenser("Yoda", "85", "Male") you would have to do this PezDispenser pezDispenser = new PezDispenser(); pezDispenser.setCharacterName("Yoda"); pezDispenser.setAge("85"); pezDispenser.setGender("Male"); and you would also have to write the setCharacterName, setAge, setGender methods as well so more code more methods which equals more time spent less performance and bigger app/game/whatever size

Lucas Santos
Lucas Santos
19,315 Points

Ahhh I see ok so it looks like the constructor helps create individual values for each instance you create instead of having a field that has the same value for all objects. Not to mention those methods would need to be static because you're accessing them straight from the class rite?

Andrew Mitchell
Andrew Mitchell
2,739 Points

You don't have to put any args in the constructor but if you do put them in it will make your life easy, but if you wanted a separate method for each you could. It just helps because when creating an object using the constructor for it. It will automatically call that constructor method so any code you have inside of it including anything that uses the args will be executed automatically.

Lucas Santos
Lucas Santos
19,315 Points

I see so in other words if I wanted to make a separate method with args for mCharacterName and not call it in the contructor I could do that rite? What's the purpose of the constructor then?

Andrew Mitchell
Andrew Mitchell
2,739 Points

"Ahhh I see ok so it looks like the constructor helps create individual values for each instance you create instead of having a field that has the same value for all objects. Not to mention those methods would need to be static because you're accessing them straight from the class rite?" yes, glad i could help you understand :)

Lucas Santos
Lucas Santos
19,315 Points

Thanks appreciate the help!