How to Declare Variables

Access Modifiers

privatevariable is visible only within the immediate class in which it is declared.
publicvariable is visible from anywhere from which the class within which it is declared is visible.
protectedvariable is are visible to classes which extend the class in which it is declared.

Other Modifiers

staticvariable retains its value after the method in which it is declared terminates.
synchronizedvariable is locked from access by other program threads while a particular thread is updating it.
finalvariable can only be assigned a value once, that is, when it is initialised just before the first pass of the method code.

The order in which the modifiers are placed in the declaration of a variable are, by convention:

The form of a complete variable-declaration is therefore as follows:

The following statement declares three interger variables x, y, z:

private static final int x, y, z;

Scope of Variables

A variable can be declared anywhere in a Java source file. It takes effect from the point at which it is declared to the end of the 'namespace' within which it was declared.
function(                         ) {

}
A namespace is the space between the braces {} of a source block. This is deemed to include the space between the brackets ( ) of the function to which the source block pertains where present. Here, the term function is used as a generic term for method, constructor, exception handler etc..
This page's parent within this Web Site. About this Web Site. Its home page. Email its Author.