Linked Questions

14 votes
10 answers

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 ...
Sal Rahman's user avatar
  • 1,544
13 votes
9 answers

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, ...
Qix - MONICA WAS MISTREATED's user avatar
6 votes
6 answers

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 ...
J.Todd's user avatar
  • 3,823
17 votes
2 answers

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? ...
Saturn's user avatar
  • 3,907
8 votes
2 answers

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 ...
Kendra Kaye Coffey's user avatar
0 votes
2 answers

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 ...
vqtuyen's user avatar
  • 15
0 votes
2 answers

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 ...
calben's user avatar
  • 611
202 votes
14 answers

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

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. ...
Saturn's user avatar
  • 3,907
-2 votes
4 answers

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 ...
user1345541's user avatar
2 votes
1 answer

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 { ...
Asynch_'s user avatar
  • 43
1 vote
1 answer

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, ...
Konrad Viltersten's user avatar
1 vote
2 answers

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 ...
evading's user avatar
  • 181