touque.ca > Education Commons > Java > Resources

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.

  1. Use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to.
  2. Avoid public fields 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.]