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 Meet Objects Constructors

João Ignácio Brito
João Ignácio Brito
1,201 Points

this./m

In:

class PezDispenser{
  private String mcharacterName;

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

  public String getCharacterName() {
    return mcharacterName;
  }

}

Wouldn't it be easier if Craig just did like this(following code)? Is there a particular reason why he did this way? I mean he would use less m's and would use less time, right?

class PezDispenser{
  private String characterName;

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

  public String getCharacterName() {
    return characterName;
  }

}

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

There's no particular reason. Craig said so on the video timestamp 5:40.

there have been many attempts at trying to be more explicit because of it. There was at one time a very popular style of prefixing your private member variables with a lower case m.

And in the code. private String mcharacterName; this line defines the private member variable, hence prefix it w/ m in its variable name, I guess m stands for "member variable".

This is just a popular variable naming convention, whether you choose to follow it is up to you, but there's no denying that picking a good variable name does help reader make senses of your code.

Personally, I don't like prefixing with m. I learned Java in my college years, back then the style was.

  public PezDispenser(String characterName) {
    this.characterName = characterName;
  }

I still think this is the best way, even though some people may find the this keyword confusing.