Java Glossary : private scope

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : P words : private scope.

private scope
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?


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 2856.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/privatescope.html J:\mindprod\jgloss\privatescope.html