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 Constructors

Adding public constructor with private variable (GoKart) ??

Could someone tell me what my constructor is lacking? Or what I did wrong... and all that lol I feel like the video explained what a constructor is well enough but didn´t explain well how to actually build the thing (or maybe I´m just slow. That´s very possible xD)

GoKart.java
public class GoKart {
  private String mColor = "red";

  public GoKart(Color) {
    private String color;
    mColor = new Color();
  }

  public String getColor() {
    return mColor;
  }
}

3 Answers

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

Hey there Jospeh,

So we can actually define a variable in an object's parameters. and then set a private field to the variable passed in

So I think what this challenge is expecting is something closer to

public GoKart(String color) {
mColor = color;
}

What is does is basically take whatever color we pass in when we call a new GoKart and assign that value to the GoKart such as .

blackKart = new GoKart("black");

Thanks, I hope this helps. If not let me know.

Maciej Torbus
Maciej Torbus
8,137 Points

Hey Joseph,

it is important to understand what constructor is. Maybe you should try watch it again and take some time ;)

You are using constructor for creating new objects.

For example, you got object Person, so pretty naturally is that while creating this object, you should give your Person name and last name.

your class looks something like this:

public class Person {
  private String mName;
  private String mLastName;

  public Person(String name, String lastName) {

      mName = name;
      mLastName = lastName; 

  }

So while creating an object somewhere else (constructor does not create new object!) your code would look something like:

Person p = new Person("John", "Doe");

It maybe a little hard topic at start, but try to follow it step by step and focus on an idea. Ask me if you got any questions. Once you understood constructors, you will be able to use it in any class!

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Normally every class has a constructor, but it is empty (no argument) if you don´t declare any class properties like "name", or "lastName". The compiler automatically provides a no-argument, default constructor for any class without declaration.

Read this:

https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

And keep learning !!!

:)