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 Meet Objects Add a Getter

Sandeep Annapareddy
Sandeep Annapareddy
3,141 Points

Getter method - Formatting Error

Hi, I added the getter method to make the private variable "color" accessible. My code shows Error in formatting getter method. Please help me with my code. Thanks

GoKart.java
class GoKart {
  private String color = "red";

  public String getcolor() {
     return color;


  System.out.printf("The colour is %s \n", color);
  }

}

2 Answers

Nivard Rikken
Nivard Rikken
5,377 Points

The print statement cannot be after the return statement because it will never be reachable.

Also the property after get should start with a capital C so getColor()

public String getColor() {
   System.out.printf("The colour is %s \n", color);
   return color;

  }
Sandeep Annapareddy
Sandeep Annapareddy
3,141 Points

Thanks a lot for your response Nivard Rikken. It worked.