Java Reference
In-Depth Information
public class Cat {
public String name;
public int weight;
}
Compiled Java code is referred to as
bytecode
, and the name of the bytecode fi le matches
the name of the class. Compiling the
Cat.java
source fi le creates a bytecode fi le named
Cat.class
.
Line Numbers
Java source fi les do not contain line numbers. However, the classes on the exam display
line numbers. If the numbering starts with a 1, then the entire defi nition of a source fi le
is being displayed. If the numbering starts with some other value, then only a portion
of a source fi le is being displayed. You will see this explanation in the instructions at the
beginning of the SCJP exam.
Java allows multiple classes in a single
.java
fi le as long as no more than one of the top-
level classes is declared
public
. The compiler still generates a separate
.class
fi le for each
class defi ned in the
.java
fi le. For example, suppose a fi le named
Customer.java
contains
the following two class defi nitions:
1. public class Customer {
2. public String name;
3. public String address;
4. }
5.
6. class Order {
7. public int partNumber;
8. public int quantity;
9. public boolean shipped;
10. }
Compiling
Customer.java
generates two fi les:
Customer.class
and
Order.class
. Note
that the
Order
class cannot be
public
because
Customer
is already
public
, nor can
Order
be
protected
or
private
because Java does not allow top-level classes to be
protected
or
private
. Therefore,
Order
must have the default access, often referred to as friendly
or package-level access, meaning only classes within the same package can use the
Order
class. (We discuss packages in the next section.)
Search WWH ::
Custom Search