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

Behavior vs State

I am having trouble understanding what exactly state and behavior is. Can someone please explain and provide an example or reference so I can gain an better understanding?

Please and thank you!

1 Answer

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Dervin,

The example I often use to describe State and Behavior of objects is a vehicle. A vehicle has state that it can indicate to you, like how much gas is in the tank. As the driver, you may make a decision based on the state, like knowing when it's time to stop and refill at the nearest gas station.

Then the car has behavior that you control as a user, like acceleration. You instruct the car to accelerate using the pedal, but it's a behavior of the car to accelerate. This would be in the form of some function/method of the class like Accelerate();.

Which brings us to another form of state - is the car moving/accelerating? Again, this is something that an object could provide as a property to let you know the current state of the car movement.

I hope this helps.