<< Chapter < Page Chapter >> Page >

Class variables are shared among all of the objects created from a given class. Stated differently, no matter how many objects are instantiated from a class definition, they all share a single copy of each class variable.

There is no analogy to a class variable in a physical radio object. Radios are installed in different cars separated from each other by thousands of miles. Therefore, there can be no sharing of anything among different physical radio objects.

(Well, that may not be entirely true. In today's technology, different radio objects could potentially share something at a common location via satellite communications, but my car radio doesn't do anything like that.)

Class variables can cause undesirable side effects

While class variables are relatively easy to use in Java, they are difficult to explain from an OOP viewpoint. Also, it is my opinion that from a good overall design viewpoint, class variables should be used very sparingly, if at all.

Therefore, for the first several modules, I will exclude the possibility of class variables in this series of modules. (I will explain the use of class variables in Java in a subsequent module.)

Reference to an array object

Now, let's get back to the instance variable named stationNumber shown in Listing 6 . Without getting into a lot of detail, this variable is also a reference variable, referring to an array object.

The array object encapsulates a simple one-dimensional array with five elements of type double . (Java array indices begin with zero, so the index values for this array extend from 0 to 4 inclusive. I will also discuss array objects in more detail in a subsequent module.)

Persistence

The array object is where the data is stored that associates the frequency of a radio station with the simulated physical button on the front of the radio.

Each element in the array corresponds to one frequency-selector button on the front of the radio. Hence, the radio simulated by an object of the Radio class has five simulated frequency-selector buttons.

The array object exists when the code in Listing 6 has finished executing. Each element in the array has been initialized to a default value of 0.0 (double-precision float value of zero) .

The setStationNumber method

Listing 7 shows the setStationNumber method in its entirety

Listing 7 . The setStationNumber method.
public void setStationNumber( int index,double freq){stationNumber[index] = freq;}//end method setStationNumber

Associates radio station with button

This is the method that is used to simulate the behavior of having the user associate a particular button with a particular radio station. (Recall that this is accomplished on my car radio by manually tuning the radio to a specific station and then holding the button down until it beeps. Your family's car radio probably operates in some similar way.)

This method receives two incoming parameters:

  • An integer that corresponds to a button number (button numbers are assumed to begin with 0 and extend through 4 in order to match array indexes)
  • A frequency value to be associated with the indicated button.

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