private scope : Java Glossary

home page P words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish 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) ©1996-2010 Roedy Green, Canadian Mind Products
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?

Example

The above code shows the various ways you can access private members and methods within a class. Basically, anything is permitted that could conceivably be permitted.

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/privatescope.html J:\mindprod\jgloss\privatescope.html
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.89]
You are visitor number 52,549.