Classes, methods, variables and constants that only methods in the same class are permitted to see and use. If you have any choice in the matter, choose private. It simplifies coding, since you know that no other class need be considered when making changes to the code. Though the fashion is not to think about efficiency, private variables and methods generate faster code. Often public getters/setters wrap private variables. This way you can later insert extra validation code if needed. Other classes cannot bypass your checks since they can’t get at the private variable directly.
If you have a variable or method that you want no one else to use, declare it private. Even programmers that extend your class won’t be able to use it. What if you accidentally attempt to use a private method when you are not supposed to. You will get an error message. What if you accidentally choose a name for your private method that is already in use by a superclass. This is perfectly legal. It is as if they had different names. The superclass will get his version, you will get yours. To methods and functions outside the immediate class, it is if your private methods were not there at all. Ditto for private variables.
private stops surprises for the base class. Guaranteed private methods won’t do anything unexpected. They can’t be overridden.
The pitfall is accidentally redeclaring a protected variable in the superclass as private. This generates no error message. Your class sees the private version which occludes the protected version. Accidental duplicate declarations happen frequently when you are refactoring code, moving declarations between levels of superclasses. They cause subtle bugs. The programs almost work. Carefully comb your code for duplicate methods and declarations and make sure they were intentional.
My approach is, if in doubt, make a method or variable private, then open the scope up later if the need arises. the tighter the scopes are, the easier a program is to maintain. However, the wider they are, the more reusable they are.
In Netscape, inner classes can’t seem to access private variables of the enclosing class. You don’t discover this until run time when you get IllegalAccess errors. There is no such thing as a private stand alone class, since you could never access it. However, there is such thing as a private inner class which might implement a public interface.
Within the class you an access the private instance members and methods unqualified names e.g. myPrivatewhere they imply this.myPrivate, with var.myPrivate., or myMethod().myPrivate. forms.
Making members and methods private helps the maintenance programmer. He can safely change those methods and members knowing no client software or subclasses could possibly be affected. private methods are necessarily final. Hence they will often run faster because of automatic inlining optimisation. On the other hand making methods private rather than protected or package/default, can be frustrating for programmers trying to extend your class.
In early versions of Java, there was a private protected scope. It has since been dropped. I think it was dropped because few people could understand the definition. It was part way between private and protected. private protected fields could be inherited but were not accessible. Mind-bending eh?
This page is posted
Optional Replicator mirror
Your face IP:[126.96.36.199]
You are visitor number|