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 Methods

Daud Zeb
Daud Zeb
701 Points

Method exposes value or expresses behaviour?

Craig says we use methods to help express the behaviour of objects while he uses method to expose the value of field characterName without letting someone set it. How do these two ideas come together? The behaviour of the PezDispenser would be things like dispensing Pez not exposing its name right?

1 Answer

Hey Daud! This is a good question. I think the issue is your getting a little too hung up on that:

We use methods to help express the behaviour of objects

We have a getName() function simply because the name field is private by default, and so we use a function to get the name. Don't think of that function as necessarily describing an intrinsic characteristic of the object, just as a helpful method.

That being said, you could argue that this, in fact, does describe the behaviour of an object. It would not be unreasonable to suggest humans, as an object, has a getName() method that can be called by asking, "What's your name?".