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

How does getColor actually work?

I barely completed the GoKart challenge in java (only by seeing a correctly coded example from a mod) and i'm am confused as to how getColor works. I worked on that challenge for over an hour and was blown away when I actually found the correct coding. Does Java have a built in color palette that returns correct syntax? I don't understand how Java knew that "purple" was a color. I feel like i'm progressing but, missing a lot of things I should know.

Mark VonGyer
Mark VonGyer
21,239 Points

hi there.

You created a public class GoKart.

in this you created a private member variable; mColor.

This means only the GoKart class can see and change the value.

Therefore in order to retrieve the color value from another class you need to create a method which returns the private mColor variable.

This method is a string,

public String getColor() { return mColor;}

later you will call on this method. to do this you would use the instance of the class (lets say I created a new goKart called cart).

cart.getColor();

This would return the color stored in the private variable from any class you like!

2 Answers

It's been a while since I did this, but what I remember for this course, it was part of the code challenges was to create a GoKart object, that took the parameter of a color in the form of a string. When you first create a GoKart object you call this line of code:

GoKart goKart = new Gokart("Purple")

This Gokart object has taken the string value of "Purple" as a parameter, not the actual color purple as you suggested. The getColor method that belongs to this GoKart returns the string that was declared on creation of the object, in this case purple. You can further understand this, if you look at your code for the GoKart class, and look at the constructor you created and the variable it takes. And then look at the output the getColor method produces, and you should notice it returns the same variable.

so as I understand it... getColor isn't actually using a color palette or some other magic, it's simply "getting" the first parameter you have under GoKart. I believe my confusion came from thinking that getColor was only for use on picking out colors and not for Strings?

I appreciate the feedback.

Exactly, the fact that this string is the name of a color means nothing to Java, but rather it's just the user that extracts meaning from this word as a color.