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)
![Julian Sanchez](https://secure.gravatar.com/avatar/6699a1409f99284710ff8e0f99a48afa?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Julian Sanchez
Courses Plus Student 2,108 PointsPutting 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
![Stephen Kilgore](https://uploads.teamtreehouse.com/production/profile-photos/5990372/micro_12998501_10207336950916928_465929768913045320_n.jpg)
Stephen Kilgore
Java Web Development Techdegree Student 2,610 PointsBasically, 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
15,021 PointsAshenafi Ashebo
15,021 PointsThank you, Stephen. You gave a nice elaboration about constructor.