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

James Barrett
James Barrett
13,253 Points

mCharacterName = characterName;?

Hi guys, I am stuck on what this particular piece of code does.

Could someone clarify what this does? I understood the hard coded part but with the introduction of the constructor I am confused as to how it is called to Example.java

Thanks, James.

2 Answers

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hello,

So, a constructor takes an argument. That means that when I instantiate the class IE:

newClass fun = new newClass(TakesArg)

What this does is create a new object and a reference to that object called fun. So whenever I want to access something in this class I need to use dot notation with the reference. IE"

fun.doSomething()

Now, when I create or instantiate the class. It takes an argument called "TakesArg". This argument is passed to the classes constructor. But then what? In order for me to be able to use that variable outside of the method that is excepting it. In this case the constructor. I need to save it as a member variable.

The constructor takes a value, needed by the class in order to work. I need to get this value, and make it available to the entire class. Because of scope, I need to make "TakeArgs" available to the entire class as some a variable.

Specifying m before the name means it's a member variable. Or class variable. When I pass "TakeArgs" into my newClass. newClass takes that variable and in the constructor, assigns it to a member variable, thus making it available to the entire class. IE:

public newClass {

    String mTakesArgs'

    public newClass(getSomething) {

        mTakeArgs = getSomething

}


action on mTakeArgs can be done outside the constructor.

This says that when newClass is created it is going to get something. That something is whatever I pass into it. In this case I am passing into it "TakeArgs"

Now, I am passing in a variable into the class via the constructor. The constructor is now going to take that variable and save it as a member variable. Which I have created a member varaible of String for "TakeArgs". Now when I pass in the varaible, I am saving it to that value.

That way much later in the class, I can use mTakeArgs because I specified it at the very top above the constructor. Meaning it's a class varaible. So the entire class has access to it. That is why it's important to specify member variables as private. To ensure only that class has access to it.

If I hadn't done anything with it the variable. I would of been a local variable. Just living inside the method. When the method is done executing, it disappears and is no longer available. When passing a constructor a variable. The only reason to do this is because you want it for something inside your class. So you always want to save it, otherwise you wouldn't pass anything in.

Does this help or have I confused you more? Please let me know and I can try my best to explain it a different way of focus on just a part.

Thanks!

James Barrett
James Barrett
13,253 Points

I think I am getting there... So from start to finish I came to this conclusion:

PezDispenser dispenser = new PezDispenser("Donatello"); This is passed to the constructor placeholder variable called characterName and in the code block this is assigned to mCharacterName... The method getCharacterName() then returns this value back to Example.java.

I'm not 100% sure what 'private String mCharacterName;' is doing. Why is it private and what relevance does it have on the code?

I think I am struggling with terminology more than anything... Words such as fields, instance, declared, initialized are still not 100% clear to me yet!

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi James!

So the mCharacterName is a private field stored in the instance. We are taking what is passed in upon creation of the instance from the constructor and setting that field.

So in this code

PezDispenser yodaDispenser = new PezDispenser("Yoda");
PezDispenser darthDispenser = new PezDispenser("Darth Vader");

The yodaDispenser's mCharacterName is set to the String "Yoda" and darthDispenser's mCharacterName will be set to "Darth"

We will later choose what we want to expose through our instance, but for now, no one can see that, or set that other than during creation of the object. This is because we made it private.

We are initializing the fields in the creation of the object.

That help clear things up?

James Barrett
James Barrett
13,253 Points

I think I am understanding it better but I am struggling to understand how important this piece of code is to the program and why it is important to declare it as private:

'private String mCharacterName;'

Craig Dennis
Craig Dennis
Treehouse Teacher

That means private to the instance, code run outside of the class will not be able to access that. This allows you to choose what implementation details you want to expose to people using your object. Like the radio example. The radio exposes knobs to change the channel and buttons to turn things off and on, but internally it stores more information that it doesn't let you change, it's part of the private way things are functioning internally.

That help?

James Barrett
James Barrett
13,253 Points

Yeah I think I am understanding it better now, I posted a response to the moderator above too! I explained what I thought was going on in the code, I hope I am on the correct path!