Java Reference
In-Depth Information
Good Programming Practice 2.1
Some organizations require that every program begin with a comment that states the pur-
pose of the program and the author, date and time when the program was last modified.
Error-Prevention Tip 2.1
As you write new programs or modify existing ones, keep your comments up-to-date with
the code. Programmers will often need to make changes to existing code to fix errors or to
enhance capabilities. Updating your comments helps ensure that they accurately reflect
what the code does. This will make your programs easier to understand and modify in the
future. Programmers using or updating code with out-of-date comments might make in-
correct assumptions about the code that could lead to errors or even security breaches.
Using Blank Lines
Line 3 is a blank line. Blank lines, space characters and tabs make programs easier to read.
Together, they're known as
white space
(or whitespace). The compiler ignores white space.
Good Programming Practice 2.2
Use blank lines and spaces to enhance program readability.
Declaring a Class
Line 4
public class
Welcome1
begins a
class declaration
for class
Welcome1
. Every Java program consists of at least one
class that you (the programmer) define. The
class
keyword
introduces a class declaration
and is immediately followed by the
class name
(
Welcome1
).
Keywords
(sometimes called
reserved words
) are reserved for use by Java and are always spelled with all lowercase let-
ters. The complete list of keywords is shown in Appendix C.
In Chapters 2-7, every class we define begins with the
public
keyword. For now, we
simply require
public
. You'll learn more about
public
and non-
public
classes in Chapter 8.
Filename for a
public
Class
A
public
class
must
be placed in a file that has a filename of the form
ClassName
.java
, so
class
Welcome1
is stored in the file
Welcome1.java
.
Common Programming Error 2.2
A compilation error occurs if a
public
class's filename is not exactly same name as the class
(in terms of both spelling and capitalization) followed by the
.java
extension.
Class Names and Identifiers
By convention, class names begin with a capital letter and capitalize the first letter of each
word they include (e.g.,
SampleClassName
). A class name is an
identifier
—a series of char-
acters consisting of letters, digits, underscores (
_
) and dollar signs (
$
) that does
not
begin
with a digit and does
not
contain spaces. Some valid identifiers are
Welcome1
,
$value
,
_value
,
m_inputField1
and
button7
. The name
7button
is
not
a valid identifier because
it begins with a digit, and the name
input field
is
not
a valid identifier because it contains
a space. Normally, an identifier that does not begin with a capital letter is not a class name.