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

Weston Ross
Weston Ross
832 Points

Im trying to add a public constructor to a class that allows the parameter to be passed in.

And in the constructor i need to store the argument color in the private field. I have followed the same pattern that has been shown in the previous video. But I am getting this error:

./GoKart.java:4: error: invalid method declaration; return type required public Gokart(String color){ ^ 1 error

Thank you for your help.

GoKart.java
public class GoKart {
  private String mColor;

  public Gokart(String color){
    mColor = color;
  }
  public String getColor() {
    return mColor;
  }
}
Pushpa Latha Rachamadugu
Pushpa Latha Rachamadugu
1,383 Points

Hi,

Pls mathc the case of the class name GoKart ( this is your class) but constructor name is differnt .so it treating as method.

Pls change it and let me know if any issues

4 Answers

Pushpa Latha Rachamadugu
Pushpa Latha Rachamadugu
1,383 Points

Hi,

Pls mathc the case of the class name GoKart ( this is your class) but constructor name is differnt .so it treating as method.

Pls change it and let me know if any issues

Vitor Freitas
Vitor Freitas
3,579 Points

The constructor need to have the same name as class, so the correct is 'GoKart' not 'Gokart'.

Weston Ross
Weston Ross
832 Points

Oh haha, silly me. I changed and it worked

Pushpa Latha Rachamadugu
Pushpa Latha Rachamadugu
1,383 Points

cool..Thanks for the updates.. Keep going...all the best