Access Modifiers
- private
- visible to the class
- [blank]: package-private
- visible to the class and the package
- protected
- visible to the class, package, and all subclasses
- public
- visible to the class, package, all subclasses, and the world
Tips on choosing an access level
The Java Tutorials (Oracle, 2012) offer these tips: “If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this.
- Use the most restrictive access level that makes sense for a particular member. Use
privateunless you have a good reason not to. - Avoid
publicfields except for constants. Public fields tend to link you to a particular implementation and limit your flexibility in changing your code.”
Oracle (2012). Controlling Access to Members of a Class. Retrieved 2012-09-30 from <http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html>.
touque.ca > Education Commons > Java > Resources
[This page last updated 2020-12-23 at 12h13 Toronto local time.]

© 2007–2025 Hersch, Bear & Company Limited. All rights reserved. “Grammar Authority,” “grammarauthority.com,” “touque.ca,” and “Canada’s Thinking Cap” are trademarks of Hersch, Bear & Company Limited. All other trademarks and copyrights are the property of their respective owners.
Privacy Policy | Licence to Copy | How to cite | Contact us
