Using a Class

In this lesson, we will look at how to use classes.

What is Class?

A class is like a plan for building cars, as the figure given below illustrates. Many cars can be built from the description given in one plan. The plan specifies that a car should have four wheels, among other things, but the actual wheels belong to the car, not the plan.

Similarly, a class is a plan for creating certain objects. It describes the objects’ data and behaviors. That is, a class contains declarations for the data associated with its objects and definitions of methods that implement their behaviors.

We begin by considering a simple class called Greeter. We will start by assuming that someone has written this class for us and then define it ourselves.

Using the Class Greeter

Each object of the class Greeter contains a greeting that it can display. For example, the program given below defines three different Greeter objects. Click the RUN button to see the results of its execution.

Press + to interact
/** GreeterDemo.java by F. M. Carrano
Demonstrates the class Greeter.
*/
public class GreeterDemo
{
public static void main(String args[])
{
Greeter standardWelcomer = new Greeter();
Greeter townCrier = new Greeter("It's 12 o'clock; all is well!");
Greeter courtClerk = new Greeter("Order in the Court!");
standardWelcomer.greet();
townCrier.greet();
courtClerk.greet();
} // End main
} // End GreeterDemo

Each object’s greeting is established at the time that the object is created, that is when the operator new executes. For example, when we define the object standardWelcomer, the expression, newGreeter(), creates a Greeter object, allocates space for a greeting, and assigns it the default value, "Hello, world!", that was chosen by the programmer who defined the class Greeter. On the other hand, the Greeter objects townCrier and courtClerk have greetings that we chose when using the class Greeter in the program GreeterDemo.

The new operator

As we pointed out in the previous chapter, new is a Java operator that creates an object of a given class type. In the code given above, the first use of new operator has one operand, the expression Greeter(). This expression represents a call to a special method, known as a constructor, within the class Greeter. A constructor initializes the newly created object. Constructors have the same name as the class. This particular constructor accepts no arguments, so we write nothing between the parentheses. Such a constructor is called a default constructor.

When we define the object townCrier, we involve another constructor. Note that a class can have several different constructors. This constructor has an argument that represents the object’s greeting as a string. So townCrier’s greeting is “It’s 12 o’clock; all is well!”

When each of these objects receives an invocation for the method greet, its own greeting is displayed, as we can see when we run the program given above. Although each object has the same method greet, a different string—the greeting associated with the object—is displayed each time.

📝 Note: The program component that uses a class is the client of the class. The user is the person using the program.