Linked Questions
13 questions linked to/from Why do we need private variables?
14 votes
10 answers
9k views
Do ALL your variables need to be declared private? [duplicate]
Possible Duplicate: Why do we need private variables? I know that it's best practice to stay safe, and that we should always prevent others from directly accessing a class' properties. I hear this ...
13 votes
9 answers
4k views
Isn't class scope purely for organization? [duplicate]
Isn't scope just a way to organize classes, preventing outside code from accessing certain things you don't want accessed? More specifically, is there any functional gain to having public, protected, ...
6 votes
6 answers
2k views
What is the usefulness of private variables? [duplicate]
While I've never used a language that had built-in variable privacy, a book I'm reading by Douglas Crockford explains a way to create privacy in JavaScript, however it doesn't make sense to me so. The ...
17 votes
2 answers
65k views
Why shouldn't I be using public variables in my Java class? [duplicate]
In school, I've been told many times to stop using public for my variables. I haven't asked why yet. This question: Are Java's public fields just a tragic historical design flaw at this point? ...
8 votes
2 answers
13k views
Why shouldn't I make variables public, but should use public getters/setters? [duplicate]
I'm watching a C++ tutorial video. It is talking about variables inside classes and assert that variables should be marked private. It explains that if I want to use them publicly, I should do it ...
0 votes
2 answers
1k views
Encapsulation and information hiding in Java [duplicate]
Member variables of a class are typically hidden from the outside word (i.e., the other classes), with private access control modifier. Access to the member variables are provided via public ...
0 votes
2 answers
249 views
Are private members useful anymore? [duplicate]
Watchpoints and data break points make it possible to watch the changes of a value in memory in many languages. Much of the justification I have seen for getters and setters and private variables ...
202 votes
14 answers
142k views
When are Getters and Setters Justified?
Getters and setters are often criticized as being not proper OO. On the other hand, most OO code I've seen has extensive getters and setters. When are getters and setters justified? Do you try to ...
-8 votes
3 answers
5k views
How can the containsKey() method of a Java Hash Table be O(1)? [duplicate]
I had a very large ArrayList in Java, and I often need to check if it contains a particular value. This has proven very slow. Then I discovered that you can use a data structure based on a Hash. ...
-2 votes
4 answers
270 views
How do access modifiers actually help programmers? [closed]
So a 'fundamental' OOP principle is being able to control the access to various methods and fields from other parts of the program by making them public or private. The reason is so that you cannot ...
2 votes
1 answer
270 views
OOP Encapsulation Philosophy
Is it fair to say that it is good practice to default everything to private up when defining a class? For example, for my public interface I would set my class something like this: class foo { ...
1 vote
1 answer
121 views
How to motivate adaptive an shifting class access modifiers
At the new client, we had a discussion regarding access modifiers for classes, methods, members etc. One opinion was to keep things as private as possible, only allowing for protected (internal, ...
1 vote
2 answers
247 views
How to design member access for an Article-type class? [duplicate]
I am doing a final project in a C++ class writing a very simple usenet-like client/server. I figure that since it's usenet-like I'll have a NewsGroup class and an Article class, I'm now working on my ...