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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![James Barrett](https://uploads.teamtreehouse.com/production/profile-photos/3674042/micro_jamesbarrett19th.png)
James Barrett
13,253 PointsmCharacterName = 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](https://uploads.teamtreehouse.com/production/profile-photos/3308302/micro_20150304_163928.jpg)
Ryan Ruscett
23,309 PointsHello,
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!
![Craig Dennis](https://secure.gravatar.com/avatar/950c867932cc1483d8aa3dad92c4cb81?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Craig Dennis
Treehouse TeacherHi 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](https://uploads.teamtreehouse.com/production/profile-photos/3674042/micro_jamesbarrett19th.png)
James Barrett
13,253 PointsI 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](https://secure.gravatar.com/avatar/950c867932cc1483d8aa3dad92c4cb81?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Craig Dennis
Treehouse TeacherThat 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](https://uploads.teamtreehouse.com/production/profile-photos/3674042/micro_jamesbarrett19th.png)
James Barrett
13,253 PointsYeah 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!
James Barrett
13,253 PointsJames Barrett
13,253 PointsI 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!