Introduction To Java-3 Classes and Objects
Introduction To Java-3 Classes and Objects
• This class includes one instance variable, named count, which will have a
default value of zero, unless we otherwise initialize it.
• The class includes two special methods known as constructors, one
accessor method, and three update methods.
© 2014 Goodrich, Tamassia, Goldwasser Java Primer 1 2
Creating and Using Objects
• Classes are known as reference types in Java, and a variable
of that type is known as a reference variable.
• A reference variable is capable of storing the location (i.e.,
memory address) of an object from the declared class.
• So we might assign it to reference an existing instance or
a newly constructed instance.
• A reference variable can also store a special value, null,
that represents the lack of an object.
• Here, a new Counter is constructed at line 4, with its reference assigned to the variable
c. That relies on a form of the constructor, Counter( ), that takes no arguments between
the parentheses.
• If there are several methods with this same name defined for a class,
then the Java runtime system uses the one that matches the actual
number of parameters sent as arguments, as well as their respective
types.
• A method’s name combined with the number and types of its
parameters is called a method’s signature, for it takes all of these
parts to determine the actual method to perform for a certain
method call.