<< Chapter < Page Chapter >> Page >

Thus, the radio object behaves in a specific way in response to a message asking it to tune to a particular frequency.

Where do objects come from?

In order to mass-produce car radios, someone must first create a set of plans, (drawings, or blueprints) for the radio. Once the plans are available, the manufacturing people can produce millions of nearly identical radios.

A class definition is a set of plans

The same is true for software objects. In order to create a software object in Java, it is necessary for someone to first create a plan.

In Java, we refer to that plan as a class .

The class is defined by a Java programmer. Once the class definition is available, that programmer, (or other programmers) , can use it to produce millions of nearly identical objects.

(While millions may sound like a lot of objects, I'm confident that since Java was released into the programming world around 1997, Java programmers around the world have created millions of objects using the standard Java class named Button .)

An instance of a class

If we were standing at the output end of the factory that produces car radios, we might pick up a brand new radio and say that it is an instance of the plans used to produce the radio. (Unless they were object-oriented programmers, the people around us might think we were a little odd when they hear us say that.)

However, it is common jargon to refer to a software object as an instance of a class .

To instantiate an object

Furthermore, somewhere along the way, someone turned the word instance into a verb, and it is also common jargon to say that when creating a new object, we are instantiating an object.

A little bit of code

It is time to view a little bit of Java code.

Assuming that you have access to a class definition, there are several different ways that you can create an object in Java. The most common way is using syntax similar to that shown in Listing 1 below.

Listing 1 . Instantiating a new Radio object.
Radio myObjRef = new Radio();

What does this mean?

Technically, the expression on the right-hand side of the equal sign in Listing 1 applies the new operator to a constructor for the class named Radio in order to cause the new object to come into existence and to occupy memory.

(Suffice it at this point to say that a constructor is code that assists in the creation of an object according to the plans contained in a class definition. The primary purpose of a constructor is to provide initial values for the new object, but the constructor is not restricted to that behavior alone.)

A reference to the object

The right-hand expression in Listing 1 returns a reference to the new object.

What can you do with a reference?

The reference can later be used to send messages to the new object (call methods belonging to the new object) .

Saving the reference

In order to use the reference later, it is necessary to save it for later use.

The expression on the left-hand side of the equal sign in Listing 1 declares a variable of the type Radio named myObjRef .

(Because this type of variable will ultimately be used to store a reference to an object, we often refer to it by the term reference variable .)

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask