scope : Java Glossary


target of concentric circles symbolising spheres of influence scope
Visibility Class scope
Gotchas Links
Limiting Scope


Java class variables have six possible levels of visibility to other classes:
Java Scope
Scope Visibility Purpose
published visible to external users of a Javabean Javabean properties and methods. There is no way to declare a method published in the Java source. You use a special getXXX/setXXX naming convention and write a separate declaration file.
public visible to all classes methods and variables of interest to users of the class.
protected visible to classes outside the package that inherit the class, also to all classes in the package. methods and variables of interests to third parties who may extend your class.
aka package
aka friendly
visible to all classes of the package. methods and variables involved in cross-class communication within the package. Java 1.8 now has a default keyword. However, it does not mean package scope. It is used in interfaces to make them behave more like abstract classes, providing default implementations.
private visible only within the class, not by inheritors, not by other classes in the package. Variables and methods that should not or would not be changed by someone extending the class. Proper functioning of the class depends on them working precisely as written.
local visible only to the block of the method in which the variables were declared. Temporary working variables.


Limiting Scope

There are five benefits to limiting the scope of indexing variables to the loop block:
  1. The optimiser does not need to worry about preserving loop index values in case you jump out of the loop. This means faster code.
  2. It may reduce the size of the stack frame. Local variables in disjoint scopes within a method can share the same slots in the stack frame.
  3. You can reuse indexing variable names safely.
  4. You are less likely to create bugs where you inadvertently reuse a variable for two different purposes.
  5. It is less confusing for programmers. They need not check for uses of the variable outside the loop.
I use the term indexing variable in the most general sense. It may not necessarily appear formally in a for or while loop.

Class scope

Permitted class scopes
Type public protected default private
standalone class
top level class
inner class
static nested class
anonymous class
local class
A top-level class is usually a default scope outer class sharing a *.java file with a public class. The term also can be used for public classes. You can have at most one public class per *.java file. A *.java file might contain only default scope classes.

This page is posted
on the web at:

Optional Replicator mirror
on local hard disk J:

Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

Your face IP:[]
You are visitor number