Java: Summary - Basic Elements


// Everything to the end of the line is ignored. Use for most comments.
/* Everything (possibly many lines) is ignored until a */. Uncommon. Use for commenting out code.
/** Used for automatic HTML documentation generation by the javadoc program. */

Identifier Names

  • Start identifiers with an alphabetic character (a-z or A-Z), and continue with alphabetic, numeric (0-9), or '_' (underscore) characters. Do not use $.
  • Second words in a name should start with an uppercase letter.
  • Do not use keywords (see below).
  • Class and interface names should start with an uppercase letter (Graphics, ActionListener, JButton, ...)
  • Variable and method names should start with a lowercase letter (repaint(), x, ...).
  • Constants should be all uppercase with underscores between words (BoxLayout.X_AXIS, Math.PI, ...).


The keywords are divided into three categories by expected student usage. Maybe try, catch, and assert are more common.

CategoryVery commonLess CommonUncommon
Primitive Types boolean char double int true false byte float short long
Control Flow if else while for switch case break default return assert do try catch continue finally throw synchronized
OOP class extends implements new null this enum interface super instanceof
Declarations public private import package static final void throws protected transient volatile native
Other strictfp goto const

Variables - Local, Instance, Class

Variables may be local, instance (field), or static (class) variables. Formal parameters are local variables that are assigned values when the method is called.
local instance static / class
Where declared In a method. In class, but not in a method. In class using static keyword.
Initial value Must assign a value before using. Compiler error if you don't. Zero for numbers, null for objects, false for boolean. Or initialized in constructor. Zero/null/false or initialized in static initializer.
Visibility Only in the same method. No visibility may be declared. private: Only methods in this class. Default: All methods in same package. public: Anyone can see it. protected: This class and all subclasses can see it. Same as instance.
When created When method is entered. When an instance of the class (object) is created with new. When program is loaded.
Where in memory Call stack. Heap. "Permanent" memory.
When destroyed When the method returns. When there are no more references to the object. When program terminates.

Copyright 2005 Fred Swartz