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

Constructor && this

Hello could someone please tell me what a constructor means and how to make one? I did not also get what the word 'this' does! thanks a lot

2 Answers

thank you Yanuar, I will read your answer a couple of times to make sure that I understand it :D

HI Mooaz

The official explanation from Oracle Java Docof what is constructor is this:

A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type. And this.<variable name> represents variable created in the class variable declaration (which for the record does not include this, in their declaration). Please read more from it if you will or if you may I can try to explain it to you using my own language style:

If you were asking about constructor and this. meaning most likely you have seen something similar like this:

class PezDispenser {
  public static final int MAX_PEZ = 12;
  final private String characterName;
  private int pezCount;

  public PezDispenser(String characterName) {//<--Here this is a constructor!!
    this.characterName = characterName;
    pezCount = 0;
  }

As you can see the class PezDispenser share the same name as the method public PezDispenser(String characterName). Therefore by definition this method is what it is called constructor to the PezDispenser class. It takes a String argument called characterName and pass it to variable this.characterName.

So in conclusion to create a constructor you need to make a method with the same name as the class it constructing and takes arguments if necessary, then pass value to the variables that initiate the class. For the record not all variable inside the constructor must be filled with value from the arguments, the developer can also set it directly or by calling another methods (a little bit confusing I know but you do not need to worry about this now).

You also ask about what is this. in the variable this.charcaterName means right? Okay let us answer that first...

Basically characterName and this.characterName is not the same thing. the characterName variable has it roots form this code:

PezDispenser dispenser = new PezDispenser("Yoda");

this code is in different file in different class from the PezDispenser class and it is calling the PezDispenser class and ask it to create a new object called dispenser which is a PezDispenser with "Yoda" character as its head, This "Yoda" then palced in the characterName variable and brought to PezDispenser class constructor as argument. Thus at this moment characterName variable holds a value = "Yoda".

So what about this,characterName? At this moment this.characterName variable still has no value stored. Then the class constructor pass that "Yoda" value to this.characterName. Then from this very moment the dispenser object created earlier will have attributes from PezDispenser class but with "Yoda" characterName as special feature it holds.

Then as described above basically constructor deals with creating special features of a class whenever the class is called to instantiate object. In that example case the constructor handle argument passed as mandatory requirement when calling the class and pass it to the correct variable inside the class. For the record you do not obligated to use this. as coding style. It just this. allows developer to use the same variable name but differentiate their functions and origins. This is a best practice to make other developer understand your code easily. However, you may build your code as this:

class PezDispenser {
  public static final int MAX_PEZ = 12;
  final private String mCharacterName;
  private int pezCount;

  public PezDispenser(String characterName) {//<--Here this is a constructor!!
    mCharacterName = characterName;
    pezCount = 0;
  }

and still produce the same process. In the future lessons this also will be recommended best practice. However, since you asked about this. I explained that first and then showing you that even if the variable inside the class have different name than the variable passed to the class as argument the class constructor will still do its job.

I hope this will help you a little. I am sorry if this is to long of an explanation.