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

Julian Sanchez
PLUS
Julian Sanchez
Courses Plus Student 2,108 Points

Putting a constructor into practice

As I didn't understand wholly in what cases may I use a constructor I ask anyone to give me a real life example please

1 Answer

Basically, constructors allow you to initialize your member variables at the time that you create an instance of an object.

Let's say I'm coding a racing game and I want to create an object to model a car.

public class Car {
    private String make;
    private String model;
    private int topSpeed;
}

If I make the above class, we see that each of my objects will have three member variables that are private. In order to populate those values, I'd need to make setter methods that are publically accessable (think methods like setMake() setModel() and setTopSpeed() )

Instead of having the user of the class call each of those setters (if they even exist) we can have all of these variables populated when the object is created by using a constructor.

public class Car {
    private String make;
    private String model;
    private int topSpeed;

     //this is our constructor
    public Car(String make, String model, int topSpeed)
    {
        this.make = make;
        this.model = model;
        this.topSpeed = topSpeed;
     }
}

notice we use the keyword this in the constructor if the variable names the constructor uses are the same as the member variables we used to set up our class. using the this keyword allows us to refer to the variable that exists on the class itself rather than the variable that is an argument of the constructor.

So now that we know how to set this up on our class, how do we use it?

When it comes time to make a new Car object somewhere else in our code, we do this:

Car myCar = new Car("Porsche", "Boxster", 177);

take a look at this portion of the above code:

new Car("Porsche", "Boxster", 177);

this is where the constructor we defined is passed the values it needs to set everything up in this.make, this.model, and this.topSpeed respectively.

The long and short of it is that a constructor is a special method called when a new instance of an object is created.

Ashenafi Ashebo
Ashenafi Ashebo
15,021 Points

Thank you, Stephen. You gave a nice elaboration about constructor.