3.14 STATIC MEMBERS OF A CLASS


3.14 STATIC MEMBERS OF A CLASS

Although a much more detailed discussion of this topic is presented in Chapter 11, we want to quickly introduce the reader here to the basic notion of a static member for a class and the syntax that is employed to access such members.

A static member is global to all the objects of a class. For example, in the C++ class

      class SavingsAccount {           string name;           double balance;      public:           static double interestRate; //(A)      }; 

all objects of type SavingsAccount will have the same value for the static data member interestRate of line (A). If the value of this data member is changed at some point, that change will affect all SavingsAccount objects—even those that were constructed prior to the change.

In C++, a public static data member can be accessed directly through the class using the scope operator ‘::’, as in

      SavingsAccount::interestRate = 6.5; 

although it could also be accessed through an object of type SavingsAccount (see Chapter 11).

A private static data member may require appropriate access functions. The following example shows the use of static member functions in lines (C) and (D) for retrieving and changing the value of the static data member of line (B): purpose:

      class X {           int m;           static int n;                                      //(B)      public:           X( int p ) { m = p; }           static int getn() ( return n; }                    //(C)           static void setn( int m ) { n = m; }               //(D)      }; 

Now the value of the static member n may be retrieved by invoking it directly against the class, as in

      X::setn( 20 );      cout << X::getn(); 

This example is not meant to imply that the value of a static data member can only be modified by a static member function; it can also be modified by a nonstatic member function. However, a static member function is not allowed to access nonstatic data members of a class.

The static members of a Java class behave in the same manner as the static members of a C++ class; however, the syntax for accessing such members is different since Java does not support the scope operator. For example, for the Java class

      class SavingsAccount {           string name;           double balance;           public static double interestRate; //(E)           // ....      } 

the static data member interestRate of line (E) may be accessed using the usual dot operator, but against the class itself, as in

      SavingsAccount.interestRate = 6.5; 

The same would apply to the static method of a Java class. To access it, you'd need to invoke via the dot operator on the class itself.

An abstract class is not allowed to have static member functions.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net