Java Reference
In-Depth Information
An instance of a class is a technical term for an existing object of that class. Ash is a specification for a
type of object and yourAsh is an object constructed to that specification, so yourAsh would be an
instance of the class Ash . Once you have a class defined, then you can come up with objects, or
instances of that class. This raises the question of what differentiates an object of a given class, an Ash
class object say, from a Redwood object. In other words, what sort of information defines a class?
What Defines a Class of Objects?
You may have already guessed the answer. A class definition lists all the parameters that you need to
define an object of that particular class, at least, so far as your needs go. Someone else might choose a
larger or smaller set of parameters to define the same sort of object - it all depends on what you want to
do with the class. You will decide what aspects of the objects you need to include to define that
particular class of object, and you will choose them depending on the kinds of problems that you want
to address using the objects of the class. Let's think about a specific class of objects.
For a class Hat for example, you might use just two parameters in the definition. You could include the
type of hat as a string of characters such as "Fedora" or "Baseball cap" , and its size as a numeric
value. These parameters that define an object of a class are referred to as instance variables or
attributes of a class, or class fields . The instance variables can be basic types of data such as numbers,
but they could also be other class objects. For example, the name of a Hat object could be of type
String - the class String defines objects that are strings of characters.
Of course there are lots of other things you could include to define a Hat if you wanted to, color for
instance, which might be another string of characters such as "Blue" . To specify a class you just decide
what set of attributes suit your needs, and those are what you use. This is called data abstraction in the
parlance of the object-oriented aficionado, because you just abstract the attributes you want to use from
the myriad possibilities for a typical object.
In Java the definition of the class Hat would look something like:
class Hat {
// Stuff defining the class in detail goes here.
// This could specify the name of the hat, the size,
// maybe the color, and whatever else you felt was necessary.
}
The name of the class follows the word class , and the details of the definition appear between the
curly braces.
Because the word class has this special role in Java it is called a keyword , and it is
reserved for use only in this context. There are lots of other keywords in Java that you
will pick up as we go along. You just need to remember that you must not use any of
them for any other purposes.
Search WWH ::




Custom Search