The point of a private variable is that you can only get it by calling a method. This means that the class is free to change the way it stores the information, without hurting other classes. Imagine we have a class:
and another class uses it like:
We now can't change the internal representation of the url. But what if we need to separate it out into the protocol (http) and the address (www,javaranch.com/)?
Imagine we had written it as:
It is now easy to change T1, without needing
any changes to the other code:
This approach of "decoupling" the implementation of a class from its use is immensly useful in making code more maintainable. I've written hundreds of thousands of lines of
Java, and I rarely use anything other than "private" for data members of classes.