Why do we need private variables?

Posted by rak on Programmers See other posts from Programmers or by rak
Published on 2012-04-10T05:31:08Z Indexed on 2012/04/10 5:42 UTC
Read the original article Hit count: 191

Filed under:
|

Why do we need private variables in classes in the context of programming?

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?

© Programmers or respective owner

Related posts about java

Related posts about c++