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 Access Modifiers

second time..

i need help

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

1 Answer

Joseph Wasden
Joseph Wasden
20,406 Points

Alright, so you have the field type of String declared, and the field name (color). We need to add an access level modifer keyword before the type keyword (in this case, String) to ensure the field's privacy.

You can find a table of access level modifiers here. https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

The two access level modifiers we want to concern ourselves with are public and private. Since we want to ensure the field's privacy, let's try the private access level modifier.

class GoKart {
  private String color = "red";
}

Llama Llama ding dong! It works! What do you think would have happened if we had used the public modifier? maybe that's something to look into...