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

David Geoffry
David Geoffry
444 Points

Need help with putting in the return method plz have the getter but not the return

idk

GoKart.java
class GoKart {
  private String color = "red";
  public String getColor();

  return color;
}

4 Answers

Gabe Olesen
Gabe Olesen
1,606 Points

If I'm not mistaken you'd need to do the following:

class GoKart { // open GoKart class
  private String color = "red";

public String getColor() { // open getColor method

  return color;

  } //close getColor method 

} // close GoKart class

Because you're creating a method, in this case "getColor()" you need to return the variable "color" inside of the "getColor()" scope thus {} are used.

Hope this helped!

Gabe

MODERATOR EDIT: Added Markdown to the code so that it is readable in the Community Response. Please refer to the Markdown Cheatsheet when posting code in the Community for the proper ways, so that it is readable.

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

watch what he does in the videos, every method looks like this (at least as to the {}):

public String myFunction(){
  [method body]
  return [String]
}

you have the right code you just need to add the {} as in my example and in the videos.

David Geoffry
David Geoffry
444 Points

could you explain it differently or something I still don't get it sorry :P