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 Data Structures Getting There Class Review

Emmet Lowry
Emmet Lowry
10,196 Points

Objects

I dont understand how the object is called in a constructor any help and insight would be great.

4 Answers

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey There Emmet, a constructor is pretty much just a blue print of the object, we're actually not creating them. Let's make a fake object Bicycle, give it string to represent the color, and an int to represent the cost

public Bicycle(String color, Int cost) { 
// we would set ant private variables to the class to being in here. Let's call it mColor and mGear
mColor = color;
mCost = cost;
}

So we actually have not created a bike object yet that we can call methods on.

We would need to do something like below.

Bicycle redBike = new Bicycle("Red", 150); 

This is where the object redBike is actually created, all the constructor does is tell our program how we want to create an object and what values we want to pass into it. It also is generally where we would set our member variables that are specific to the Class.

now that we've done that we can actually call methods on our bike let's assume we made getter's for both of the variables.

redBike.getColor();

redBike.getCost();

we had to follow a step by step process, create the class, create the member variables, create the constructor of what the objects should be like, and then finally create an instance of that object. Until we finally reached the redBike step a new object hadn't been declared, and nothing for us to call our methods on.

Thanks I hope this helps, as always give me a shout if it doesn't.

Emmet Lowry
Emmet Lowry
10,196 Points

thanks for answer rob one question is the bicycle before redBike a type or what is it and what does blue print mean thanks.

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey Emeet, a blue print is basically a design on how you will build something before you actually create it. Such as an architect would design it on paper before actually building a structure.

The Bike before redBike is actually the Object of Bike. redBike is just an instance of the bike Object.

Thanks I hope this helps.

SungGeun Kim
SungGeun Kim
9,072 Points

What constructor does is to take the input parameters and create an object in the memory and return the object. What we then do is to create a reference for that object and make it point to that object. For example, in the code below:

Treet treet = new Treet();

The "new" keyword creates a memory space for our new Treet object and the right-hand-side treet is a reference of a Treet type which points to the newly created object.