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

Leen Leenaerts
Leen Leenaerts
1,096 Points

I'm getting the error : ./GoKart.java:4: error: invalid method declaration; return type required...

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

I don't see why I need to return something to set the Color of the Gokart.

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

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

  public String getColor() {
    return mColor;
  }
}

2 Answers

Hello Leen!

If you don't think you need to return anything then put the keyword 'void' where you would usually put what the function is going to return.

Void is a special keyword in Java which signifies that the function isn't going to return anything.

I hope this helped you out and good luck with your programming!

-Luke

Leen Leenaerts
Leen Leenaerts
1,096 Points

It was supposed to be a constructor so I just needed to put the keyword "GoKart". Thanks for learning me "void".

No problem! I am glad you managed to get your problem solved.