<< Chapter < Page Chapter >> Page >

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) . In Java, we refer to the equal sign (=) as the assignment operator.

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.

Calling a method on the object

Assume that the definition of the Radio class defines a method with the following format (also assume that this method is intended to simulate pressing a frequency-selector button on the front of the radio) :

public void playStation(int stationNumber)

What does this mean?

Generally, in our radio-object context, this format implies that the behavior of the method named playStation will cause the specific station identified by an integer value passed as stationNumber to be selected for play.

Public and void

The void return type means that the method doesn't return a value.

The public modifier means that the button can be pressed by anyone in the car who can reach it.

(Car radios don't have frequency-selector buttons corresponding to the private modifier in Java.)

The method signature

Continuing with our exposure of jargon, some authors would say that the following constitutes the method signature for the method identified above:

playStation(int stationNumber)

A little more Java code

Listing 2 shows the code from the earlier listing, expanded to cause the method named playStation to be called.

Listing 2 . Calling the playStation method.
Radio myObjRef = new Radio(); myObjRef.playStation(3);

The first statement in Listing 2 is a repeat of the statement from the earlier listing. It is repeated here simply to maintain continuity.

Method invocation syntax

The second statement in Listing 2 is new.

This statement shows the syntax used to send a message to a Java object, or to call a method on that object (depending on whether you prefer OOP-speak or Java-speak) .

Join the method name to the reference

The syntax required to call a method on a Java object joins the name of the method to the object's reference, using a period as the joining operator.

(In this case, the object's reference is stored in the reference variable named myObjRef . However, there are cases where an object's reference may be created and used in the same expression without storing it in a reference variable. We often refer to such an object as an anonymous object.)

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