Java Reference
In-Depth Information
OBJECT
records
methods
CLASS
fields
constructor
OBJECT
records
methods
Figure 5.1 Visualizing the class fields
and the object records when creating
object instances of a class
Thus it might be the case that some instructions try to access the records
of an object that is not yet created or initialized. This will raise an excep-
tion nullPointerException and provoke an abnormal program termination.
Indeed, objects not yet created have by default value null .The null object
is common to all classes and is used as the default value of object variables
not yet created. To avoid nullPointerException exception, it is therefore
recommended to test whether the object is null or not, as follows:
Student stud=null;
...
if (stud!=null)
stud.group=2;
5.2.3 Static (class) functions with objects as arguments
Objects can also be parameters of functions. That is, a static function may
have both primitive and non-primitive type objects. The generic prototype of
a function having object arguments is:
static TypeF F(Object1 obj1, ..., ObjectN objN)
For example, we may declare the following class function static boolean
isBefore (Date d1, Date d2) . Functions may also return an object as a
result as in static Date readDate() . The program below demonstrates these
flexibilities:
 
Search WWH ::




Custom Search