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 (Retired) Meet Objects Creating New Objects

Rodrigo Alarcon
Rodrigo Alarcon
8,028 Points

Java Object... Again

Please help me.

Now that you've created the GoKart class. Please create a new GoKart object using the GoKart class, or blueprint. As you know it takes a single parameter, color when being built.

Example.java
public class Example {

    public static void main(String[] args) {
        System.out.println("We are going to create a GoKart");
    }
}

2 Answers

OK Rodrigo,

So, as in the previous question you asked; here we put the constructor into practice.

We have the constructor:

  public GoKart(String color){  // passes in a string called color inside the method
    mColor = color; // assign color to mColor for the instance of GoKart
  }

So now we use it. The constructor takes a string as a color but, mainly, it is creating a new instance of GoKart.

That can look like:

public class Example {

    public static void main(String[] args) {
        System.out.println("We are going to create a GoKart");
        GoKart aNewKart = new GoKart("red");
    }
}

So, to the left of the equals sign we allocate a GoKart-sized hole in memory, then to the right of the = sign we create a new GoKart and put that in the memory hole we created. Oh, and we made it a 'red' GoKart.

Make sense?

Steve.

Tristan Smith
Tristan Smith
3,171 Points

Hi Rodrigo,

It wants you to create an object. I suggest reviewing the portion of the video that covers creating an object. Simply put, you create an object by calling the class constructor and including the required parameter/s or none at all, if it doesn't require parameters.

Rodrigo Alarcon
Rodrigo Alarcon
8,028 Points

Can you please explain to me in a bit more detail how to write this?

Tristan Smith
Tristan Smith
3,171 Points

I could, but then it wouldn't benefit you. :/ It would be me telling you how to exactly do it. That kind of thing doesn't really help to learn. What specifically are you having trouble with understanding in this section?

Edit:

But if you review the video once more, it covers what a constructor is, and how to [create an object using it].

Tristan Smith
Tristan Smith
3,171 Points

So... is this how the community here works? I get down voted b/c I didn't solve the task?[1]

edited [1] removed "for you?"

Rodrigo - please don't down-vote unless you are given incorrect advice. Tristan gave you a very helpful response - can you remove the down-vote, please.

Thank you, in advance.

Steve.

Tristan; I +1'd you - I hope Rodrigo does the same. No, this is not how this community works; I am sorry this is an early perception you have been given. Apologies for that.

Steve.

Rodrigo Alarcon
Rodrigo Alarcon
8,028 Points

I am sorry, but I did not vote him down. I actually used his advice and it helped a lot, I don't know who did it though.

Thanks Rodrigo - the down-vote has been removed. I did come across this the other day when a negative review appeared briefly, then vanished again.

As long as we're all happy and progressing, let's just move on and keep going! :+1:

Steve.

Tristan Smith
Tristan Smith
3,171 Points

Forgive me Rodrigo for assuming it was you. That threw me off. :P I changed my response to not seem so direct.

Rodrigo Alarcon
Rodrigo Alarcon
8,028 Points

Its ok, I see why you may have thought it was me. I probably would have done the same thing. :p