<< Chapter < Page Chapter >> Page >

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 being 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 .)

What does this mean?

Declaring a variable causes memory to be set aside for use by the variable. Values can then be stored in that memory space and accessed later by calling up the name given to the variable when it was declared.

Assignment of values

The equal sign in Listing 1 causes the object's reference returned by the right-hand expression to be assigned to, or saved as a value in, the reference variable named myObjRef (created by the left-hand expression) .

Memory allocation

Once the code in Listing 1 has finished execution, two distinct and different chunks of memory have been allocated and populated.

One (potentially large) chunk of memory has been allocated (by the right-hand expression) to contain the object itself. This chunk of memory has been populated according to the plans contained in the definition of the class named Radio .

The other chunk of memory is a relatively small chunk allocated (by the left-hand expression) for the reference variable containing the reference to the object.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Accessible objected-oriented programming concepts for blind students using java. OpenStax CNX. Sep 01, 2014 Download for free at https://legacy.cnx.org/content/col11349/1.17
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?

Ask