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 (Retired) Meet Objects Constructors

Dean Onizuka
Dean Onizuka
4,753 Points

what exactly is a member variable again?

so whats a member variable again? and how is it different from a regular variable? i know he went over it in the video... that part just slipped my mind lol

2 Answers

Saad Anjum
Saad Anjum
6,408 Points

A 'member variable' is a variable that is defined inside an object and is accessible by all its methods.

There are other types of variables as well:

  • 'local variables' which is defined inside a block of code or a method. 'Local variables' are not accessible by other methods of the class.
  • 'Parameters' are variables inside a method declaration. These are not accessible by other methods of the class either.

They are all variables.

arabasz
arabasz
1,949 Points

So it's like a variable that's a member of a club? While other variables don't need to belong to a club?

For example, I might make a member variable named "fruitColor" that is a member of the object "fruit". No other objects should have a "fruitColor" because my other objects are things like "vegetables" and "utensils". Membership is not allowed because it wouldn't make sense.

Er... does that analogy work?

A normal variable is something you define ANYWHERE..

member variable is also a variable BUT you define it inside a class (e.g. class property so animals can have a property called legs etc)..

btw: not nitpicking but can people start to use google and then post here if they don't understand or get the answers..