In Java, fields and methods always exist in the context of a class, and never in isolation as in most previous programming languages.
class ClassName // class declaration
{ // start of class definition
field declarations // zero or more data fields
method declarations // zero or more methods
} // end of class
(Note: A single-line comment in Java starts with //.
Everything after the // to the end of the line is
ignored by the compiler and other Java Development Kit tools.)
class ClassName { // class declaration
{ // start of class definition
field declarations // zero or more data fields
method declaration
{ // start of method body block
statements // zero or more statements
} // end of method body block
} // end of class
abstract class ClassName // abstract class declaration
{ // start of class definition
field declarations // zero or more data fields
method declaration
{ // start of method body block
statements // zero or more statements
} // end of method body block
abstract method decl ; // semicolon implies null body
} // end of class
interface InterfaceName // interface declaration
{ // start of interface definition
abstract method decl ; // zero or more abstract methods
} // end of interface
package
classes
interfaces
Java program
packages
These three Syntax Summaries are not, by themselves, useful for learning the Java environment programming language. However, they are useful for quick reference once the basics of the language are understood.
The Summaries include all of the keywords and options that might possibly be used in a syntactical construct, including mutually exclusive syntax. One must understand the Java language to know the appropriate syntactical combinations - the Summaries do not differentiate. The basic premise of the Summaries is that it is easier to delete written syntax that is not needed than to remember needed syntax that is not yet written.
These three Syntax Summaries comprise the declaration and statement syntax for Java version 1.0.2 (JDK 1.0.2). Additional syntax for a construct called "inner classes," added to the language with JDK 1.1, is not included due to the controversial nature of inner classes.
Class Declaration Syntax Summary
package this_package_name; // package declaration
import ext_package_name.ExtClassName; // refer to class by its simple name
import ext_package_name.*; // refer to all classes in pkg by simple name
// Class Declaration Statement
public abstract final synchronized // class declaration modifiers
class ClassName // name of class
extends SuperClassName // name of superclass
implements InterfaceName // interfaces implemented by class
{ // start of class specification
// Constant Field Variable Declaration
private static final // constant declaration modifiers
data_type_name FIELD_NAME // data type and name of field
= const_expr; // field initialization expression
// Constant Field Array Declaration
private static final // constant declaration modifiers
data_type_name[] FIELD_NAME // data type and name of field
= {const_expr, ... }; // array initialization expression
// Field Variable Declaration
private transient volatile // field declaration modifiers
data_type_name field_name // data type and name of field
= const_expr; // field initialization expression
// Field Array Declaration
private transient volatile // field declaration modifiers
data_type_name[] field_name // data type and name of field
= {const_expr, ... }; // array initialization expression
static { // Static Initializer
statements; // see Method Body Syntax
} // end of static initializer
// Constructor Definition
public|protected|private ClassName // visibility and name of constructor
(data_type_name arg_name, ...) // argument list declaration
throws exception_name // declaration of exceptions thrown
{ // start of constructor body
super (argument_list); // call to superclass constructor
this (argument_list); // call to other local constructor
statements; // see Method Body Syntax
} // end of constructor body
// Method Definition
public|protected|private // method visibility modifiers
static final synchronized native // other method declaration modifiers
data_type_name|void methodName // method return type and name
(data_type_name arg_name, ...) // argument list declaration
throws exception_name, ... // declaration of exceptions thrown
{ // start of method body
statements; // see Method Body Syntax
} // end of method body
// Abstract Method Declaration
public|protected|private // method visibility modifiers
abstract // other method declaration modifiers
data_type_name|void methodName // method return type and name
(data_type_name arg_name, ...) // argument list declaration
throws exception_name, ... // declaration of exceptions thrown
; // null (missing) method body
// Main Method Definition
public static void main // main method modifiers and name
(String[] args) // main method argument list
{ // start of method body
statements; // see Method Body Syntax
} // end of method body
} // end of class specification
As a convenince, this Class Declaration Syntax Summary has been put into the form of a Class Coding Template.
Interface Declaration Syntax Summary
package this_package_name; // package declaration
import ext_package_name.ExtClassName; // refer to class by its simple name
import ext_package_name.*; // refer to all classes in pkg by simple name
// Interface Declaration Statement
public interface InterfaceName // interface visibility and name
extends SuperInterfaceName, .. // name of superinterfaces
{ // start of interface specification
// Abstract Method Declaration
public abstract // method declaration modifiers
data_type_name|void methodName // method return type and name
(data_type_name arg_name, ...) // argument list declaration
throws exception_name, ... // declaration of exceptions thrown
; // null (missing) method body
} // end of interface declaration
As a convenince, this Interface Declaration Syntax Summary has been put into the form of an Interface Coding Template.
// Block
{ // start of block
VariableDeclarations_opt // block may contain var declarations
Statements_opt // and statements, or may be empty
} // end of block
// Local Variable Declarations
type var_name; // creates uninitialized primitive var
Type var_name; // creates uninitialized object ref var
Type[] var_name; // creates array object ref variable
type var_name=init_value_expr; // creates prim var with initial value
Type var_name=init_value_expr; // creates obj ref var and obj with value
Type[] var_name = new Type[n]; // creates empty array of length n
Type[] var_name = {const,..const}; // creates array with initial values
// Statement
Block // blocks can be nested
; // empty statement, does nothing
expr; // expression is statement if ; added
// Flow Control Statements
if (expr) Statement // simple if statement, expr type boolean
if (expr) Statement else Statement // if-else stmt, 1st stmt not simple-if,
// 2nd stmt executed if expr false
switch (expr) { // expr type char, byte, short, or int
case expr_const: // multiple cases may share one stmt
case expr_const: Statement // expr_const assignable to switch expr
break; // prevents falling thru to next case
default: Statement // default case optional, may be only one
} // end of switch
while (expr) Statement // expr type boolean, stmt executed 0 or
// more times until expr false
do Statement while (expr); // expr type boolean, stmt executed 1 or
// more times until expr false
for (init_expr_opt; // init expression executed first, once
expr_opt; // expr type boolean, use break if none
update_expr_opt) Statement // expr-Statement-update_expr sequence
// runs 0 or more times until expr false
identifier: Statement // ident label used with break,continue
// Statement must be while, do, for
break; // xfer to enclosing switch,while,do,for
// and complete stmt normally
break identifier_opt; // xfer to labeled while, do, or for and
// complete stmt normally
continue identifier_opt; // xfer to enclosing (or labeled) while,
// do, or for and begin next iteration
return; // xfer to caller (constructor,void meth)
return expr; // xfer to caller of method that returns
synchronized (ref_expr) Block // lock object referenced by expr before
// executing Block
// Exception Handling Statement
try Block // 1st matching catch executed if Block
// throws exception, exception xfers to
// caller if no catch block matches
catch (Type identifier) Block // Type is subtype of Throwable, must
: // match exceptions thrown by try block
catch (Type identifier) Block // catch clause optional
finally Block // always executed unless System.exit(n)
// executed in try or catch Blocks, opt
throw expr; // xfer to try stmt that catches expr value
Note on Inner Classes
An e-mail discussion among Java instructors (in which
L participated) revealed
that most of the instructors believed inner classes should be used sparingly.
L firmly believes mere mortals should avoid
them entirely.