Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?".