I've been trying to learn more about access modifiers in java, and everyone has said "Use private to hide things that you don't want other classes / methods using" and I'm just left wondering why that needs to be done. I can't think of a single case where private would make sense to use as a sole developer...
That issue extends to not mentioning an access specifier too. It seems like it's just limiting the flow of information, since the user has no way of seeing the code that we're generating...
Any help on understanding th开发者_Python百科is better would be greatly appreciated since I tend to learn better from programming anyways. Thanks a ton!
I can't think of a single case where private would make sense to use as a sole developer...
Even as a sole developer you can make mistakes, think of code you touch/use months after writing it. Using correct access modifiers helps with encapsulation, structuring code and avoiding mistakes (like accidently changing some class' internal state).
That issue extends to not mentioning an access specifier too. It seems like it's just limiting the flow of information, since the user has no way of seeing the code that we're generating...
I can't quite follow you here but you always mention an access modifier, not writing one just means default (package private) access. Also note that users of your code (in case of libraries etc.) will see all members and their access modifiers and they might try and access some fields that should not be modified directy if access to those fields wasn't restricted.
One simple example why you would want to hide stuff:
class CreditCard {
private int number;
public Boolean setNumber(int nr) {
if(!checkNumber(nr))
return false;
number = nr;
return true;
}
private Boolean checkNumber(int nr) {
...
}
}
I think one good way to find examples is to go to your JDK, find the src.zip file, and start browsing the source. I'm sure you'll find good citations. It might be more meaningful than anything you'll see posted here, and you'll know that it's running in production all over the world.
This would also hold for good open source projects as well (e.g. Spring).
Reading other people's good code is a great way to learn about design ideas.
Think about variables that control a state.
Lets say we have a states for a class:
bool isDead = false;
bool isAlive = true;
bool canWalk = true;
void Kill() { isDead = true; isAlive = false; canWalk = false; }
void Resurrect() { isDead = false; isAlive = true; canWalk = true; }
Now the states are strongly connected, and you don't want to be able to change one without another.
It won't mean anything if you're both dead and alive.
So you would want to make the variables private, so you don't have access to changing them. Instead you will use the functions that change the state.
You would use private for data members you want to be sure that only your class will have access too and be able to modify. You have to imagine your class being used by many other classes. If a data member is public, any of these other classes can modify it, and your class should be expecting this. If this behaviour is unexpected for that data member (it shouldn't be changed by any other code), make it private.
For example, suppose you have a data member 'count' in your class that should only be updated within your class, declare it private, otherwise stuff like this can happen:
class A
{
public int count;
public A()
{
count = 1;
}
public int divideByCount(int value)
{
return value/count;
}
public incrementCount()
{
count = count + 1;
}
}
// somewhere else
example = new A();
example.count = 0;
example.divideByCount(4); // Divide by zero!
You have to use private
modifier when you have to want that your members, such as attributes or methods, are accessible only to the current class.
The choice of using private
modifier is a task of the programmer.
If the programmer doesn't want that a member of a class cannot be accessed by other classes,because it can change a state of an object (and the programmer doesn't want that the object changes its state unexpectedly), then he uses private
.
See this page for other info on access modifiers. Check this and this too.
Even sole developers will use tools like an IDE and a compiler to help them develop.
When you mark a field as private you are saying you don't expect that field to be use anywhere else. Over time you can come back to your code and you forgot any assumptions you might have made when you first write it. If its left public, but you didn't develop it that way, you can use it incorrectly. Marking it private ensures the compiler will help you make the right choices later.
When you mark a field or method as private and it is not used anywhere, the IDE will immediately let you know this. If you make it public, it cannot help you and you can end up with lots of code which isn't used. This can be a problem if a) you expect it to be used but it is not, b) you maintain it when say you change one of its dependencies, but since its never used, this is wasted effort.
精彩评论