Statements – Declarations

3.3 Statements

Statements in Java can be grouped into various categories. Variable declarations, optionally specified with an initialization expression, are called declaration statements (p. 102). Other basic forms of statements are control flow statements (Chapter 4, p. 151) and expression statements.

An expression statement is an expression terminated by a semicolon (;). Any value returned by the expression is discarded. Only certain types of expressions have meaning as statements:

A solitary semicolon denotes the empty statement, which does nothing.

A block, {}, is a compound statement that can be used to group zero or more local declarations and statements (§6.6, p. 354). Blocks can be nested, since a block is a statement that can contain other statements. A block can be used in any context where a simple statement is permitted. The compound statement that is embodied in a block begins at the left curly bracket ({) and ends with a matching right curly bracket (}). Such a block must not be confused with an array initializer in declaration statements (p. 119).

Labeled statements are discussed in §4.10, p. 179.

3.4 Variable Declarations

A variable stores a value of a particular type. A variable has a name, a type, and a value associated with it. In Java, variables can store only values of primitive data types and reference values of objects. Variables that store reference values of objects are called reference variables (or object references or simply references).

We distinguish between two kinds of variables: field variables and local variables. Field variables are variables that are declared in type declarations (classes, interfaces, and enums). Local variables are variables that are declared in methods, constructors, and blocks. Local variables declared with the var type name are discussed in §3.13, p. 142.