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

Seán Kennedy
Seán Kennedy
899 Points

I don't understand methods or 'getters'. I can't do this exercise.

No matter how many times I watch the video, I just can't wrap my brain around it. I can't do this exercise at all.

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

  public getColor = "red";
}

1 Answer

Hi Seán,

I understand your frustration, I will make an attempt to explain:

Imagine you are in a restaurant and you want to "get" food X. The person will make an order for you, they will make the food, my you are going to "get" your food and eat it happily.

Now the analogy, you really don't care the person makes the order, and lastly how the food is being made. You hope it is clean and healthy, but you really can't tell. This is what "getters" provide in this context. Given an arbitrary object (the kitchen in our case), you can have getters, the action to get the food, for example, kitchen.getFood(), and inside these getters, many things can happen or nothing at all it all depends. For example, you may have a getter that only gets the amount of many made today which is an easier task than making an entire meal.

The idea and benefits here are the levels of abstraction that you get from these methods. So now what is a method? If you are familiar with functions, you can think of a method as a function only associated or called via an instance of an object. If this is unfamiliar to you, think about of an instance as an annual object. For example, there are many Toyota cars built from the same blueprint. But my Toyota car is not the same as someone else. Thus, my car is an instance of a Toyota car and someone else Toyota, is another instance.

Make sense? If not, please feel free to follow up.

-Dan