693
Declaration commentsaccompany the field declarations in a class. Anywhere that an iden-
tifier is declared, it is helpful to include a comment that explains its purpose. For example:
// Class constants
static final String FIRST = "Herman"; // Person’s first name
static finalString LAST = "Herrmann"; // Person’s last name
static finalchar MIDDLE = 'G'; // Person’s middle initial
// Instance variables
JFrame outputDisplay; // Output frame
String firstLast; // Name in first-last format
String lastFirst; // Name in last-first format
intstudentCount; // Number of students
intsumOfScores; // Sum of their scores
longsumOfSquares; // Sum of squared scores
doubleaverage; // Average of the scores
floatdeviation; // Standard deviation of scores
chargrade; // Student’s letter grade
String stuName; // Student’s name
Notice that aligning the comments gives the code a neater appearance and is less distracting.
In-line commentsare used to break long sections of code into shorter, more comprehensi-
ble fragments. It is generally a good idea to surround in-line comments with blank lines to
make them stand out. In this text we save space by printing the in-line comments in color
rather than using blank lines. Some editors also color comments automatically, which makes
it easier to spot them on the screen. However, blank lines are still helpful because code is of-
ten printed on paper in black and white. Here is an example:
// Instantiate labels and input field
resultLabel = newJLabel("Result:");
register = newJLabel("0.0", Label.RIGHT);
entryLabel = newJLabel("Enter #:");
inputField = newJTextField("", 10 );
//Instantiate button objects
add = newJButton("+");
subtract = newJButton("-");
clear = newJButton("Clear");
// Register the button listeners
add.addActionListener(operation);
subtract.addActionListener(operation);
clear.addActionListener(clearOperation);