Skip to main content
3 of 7
deleted 30 characters in body
mwallace
  • 2.5k
  • 3
  • 15
  • 10

Why do we need private variables?

Why do we need private variables in classes?

Every book on programming I've read says this is a private variable, this is how you define it but stops there.

The wording of these explanations always seemed to me like we really have a crisis of trust in our profession. The explanations always sounded like other programmers are out to mess up our code. Yet, there are many programming languages that do not have private variables.

  1. What do private variables help prevent?

  2. How do you decide if a particular of properties should be private or not? If by default every field SHOULD be private then why are there public data members in a class?

  3. Under what circumstances should a variable be made public?

mwallace
  • 2.5k
  • 3
  • 15
  • 10