Linked Questions

14
votes
10answers
6k 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 ...
13
votes
9answers
3k 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
6answers
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 ...
14
votes
2answers
49k 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
2answers
4k 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
2answers
962 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
2answers
200 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 ...
174
votes
16answers
123k 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
3answers
2k 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. ...
1
vote
1answer
91 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, ...
2
votes
1answer
146 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
2answers
214 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 ...