Java Reference
In-Depth Information
method should not return a reference to a mutable object. Instead, use a copy con-
structor (or other means) to return a reference to a deep copy of the mutable object.
Packages are Java's version of class libraries.
Java comes with a program named javadoc that automatically extracts the inter-
face from all the classes in a package or from a single class definition.
Answers to Self-Test Exercises
1.
Yes, it is legal, although it would be preferable style to use the class name
RoundStuff in place of roundObject .
2.
No, all methods in the class are static, so there is no need to create objects. If we
follow our style rules, no constructors would ever be used, so there is no need for
constructors.
3.
Yes, a class can contain both static and nonstatic (that is, regular) methods.
4.
You cannot invoke a nonstatic method within a static method (unless you create
an object to serve as the calling object of the nonstatic method).
5.
You can invoke a static method within a nonstatic method.
6.
You cannot reference an instance variable within a static method, because a static
method can be used without a calling object and hence without any instance variables.
7.
Each object of a class has its own copy of each instance variable, but a single copy
of each static variable is shared by all objects.
8.
No, you cannot use an instance variable (without an object name and dot) in the
definition of a static method of the same class. Yes, you can use an instance vari-
able (without an object name and dot) in the definition of a nonstatic method of
the same class.
9.
Yes, you can use a static variable in the definition of a static method of the same
class. Yes, you can use a static variable in the definition of a nonstatic method of
the same class. So, you can use a static variable in any method.
10.
No, you cannot use either an explicit or an implicit occurrence of the this
parameter in the definition of a static method.
11.
All methods with the following names could and should be marked static :
dateOK , monthOK , and monthString.
12.
All static variables should be marked private with the exception of one case: If
the static variable is used as a named constant (that is, if it is marked final ), then
it can be marked either public or private depending on the particular situation.
13.
They can both be named by using the class name and a dot, rather than an object
name and a dot.
Search WWH ::




Custom Search