private scope : Java Glossary
home P words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 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 (all)
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_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.18] Spread the Net
You are visitor number 28,228.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/privatescope.html J:\mindprod\jgloss\privatescope.html