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 Creating Classes

Challenge 2 of task 2 'Meet Objects'

i really need help with this one i have attempted it many times though

4 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Zamy;

Welcome to Treehouse!

After Task 1 you should have:

public class GoKart {

}

Task 2 asks Now let's temporarily add a public field that we will use to store the color of the GoKart. Let's make it the set to the String of "red".

To declare a public string in Java we would use public String. Let's name our variable something descriptive, like color. Since it is a member variable, it needs to start with an 'm' and color needs to be capitalized. We then need to set the color to "red" per the challange. Our code then should look like:

public String mColor = "red";

Hopefully that helped, if not post back with further questions.

Ken

it didnt work i typed that other one up for task 2 and it said syntax errors

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Abby;

I don't see where Craig defines a second class, or inner class. At about 5:30 in the video before this challenge Craig has a line of code similar to what you are saying with:

public class PezDispenser {
    public String mCharacterName = "Yoda";
}

In terms of the use of m, it is a popular naming convention to designate class variable names versus local variable names. If you just use Color, in this example, the challenge isn't giving you a syntax error, exactly. It is checking to make sure that a variable named mColor is defined and leaving the m off doesn't meet that criteria.

Class vs. local variable names can cause some confusion and if you have additional questions I would suggest starting a new thread on that issue.

Happy coding,
Ken

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Zamy;

Did you put the variable declaration inside the class? It should be between the curly brackets.

Ken

Why are we not adding a class into the second half of the code: public class String mColor = "red"; like in the example video that was gone over before this code challenge?

public class PezDispenser {
  public class String mCharactername = "Yoda";
}```

Also why must we use the m in front of mColor?  The code will throw a syntax error if you just say Color...why is that?  Why is declaring the m so important?  Thanks for your help.