Search This Blog

When should I make an instance variable private?

There are no hard and fast rules, but here are two general principles. If an instance
variable is to be used only by methods defined within its class, then it should be made private. If an instance variable must be within certain bounds, then it should be private and made available only through accessor methods. This way, you can prevent invalid values from being assigned.