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 Privacy and Methods

Farouk Charkas
Farouk Charkas
1,957 Points

I need help on this task!

This is the task link (https://teamtreehouse.com/library/java-objects/meet-objects/privacy-and-methods-2), DO NOT TELL ME THE ANSWER, I would like to learn this on my own, but I need someone to walk me through this

GoKart.java
public class GoKart {
  public String mColor;

  public String mColor = getColor
}

1 Answer

Victor Learned
Victor Learned
6,976 Points

So you want to enable access to the variable mColor. Typically this is done but making a public method that will return a variable. In your code above you are using the incorrect syntax and are half declaring a method and trying to assign it at the same time. Here's an example of a random method I created to help you understand. Try and use it to make a method that returns the variable mColor to complete the challenge.

public class GoKart {
  public String mColor = "red";

  //method example
  public Int GiveRandomInt()
  {
      return (int)Math.random()
  }
}
Farouk Charkas
Farouk Charkas
1,957 Points

Thank you very much Mr. Learned, I appreciate you taking your time answering me!