11.4 STATIC MEMBER IN JAVA


11.4 STATIC MEMBER IN JAVA

We will start by writing a Java program that imitates the first C++ program, Static1.cc, we showed in the previous section. We have a class X whose data members are of type int, static int, and static final int.

For the initialization of the data member q in the C++ program, we used the fact that C++ allows in-class initialization if a data member is of type static const. But there we left a similar data member p uninitialized inside the class to show that even a static const data member can be initialized outside the class.

For the case of Java, a static final data member must be provided with an in-class initialization. So the C++ option of initializing a static const data member outside a class does not exist in Java.

The program below shows in lines (A) and (B) the two different ways of providing the required in-class initializations for the two static final data members of X. While we provide a value directly for the initialization in line (A), we invoke a static method init for the initialization in line (B). Invoking methods in this manner is allowed for the in-class initialization of all data members, static or nonstatic, in Java.

 
//Static1.Java class X { public int m; public static int n = 100; public static final int p = 50; //(A) public static final int q = init(); //(B) public X( int mm ) { m = mm; } public static int init() { return 200; } } class Test { public static void main(String[] args) { System.out. println( X.n ); // 100 //(C) System.out. println( X.q ); // 200 X xobj_1 = new X( 20 ); //(D) System. out. println(xobj_1.m + " " + xobj_1.n); //20 100 X xobj_2 = new X( 40 ); System.out.println(xobj_2.m + " " + xobj_2.n); //40 100 X.n = 1000; //(E) System.out.println(xobj_1.m + " " + xobj_1.n); //20 1000 System.out.println(xobj_2.m + " " + xobj_2.n); //40 1000 } }

As was the case for C++, the most common way to access a static data member in Java is through the class directly, as by the following invocation in line (C) above:

      X.n 

However, a static data member can also be accessed through the objects of the class, as was also the case for C++. The following invocation in the statement after line (D) demonstrates this:

      xobj_1.n 

Also note that when we change the value of the static member in line (E), this change is reflected in the value of n as seen through the objects xobj_1 and xobj_2 constructed prior to line (E). This is demonstrated by the output produced by the statements after line (E).

Let's now consider the case when a static data member is private and see what that entails as to how we would retrieve the static member and how we would change its value:

 
//Static2.java class X { private static int n = 100; //(A) public int m; public X(int mm) { m = mm; } public static int getn() {return n;} //(B) public static void setn(int nn) { n = nn; } //(C) } class Test { public static void main(String[] args) { System.out.println(X.getn() ); // 100 //(D) X xobj_1 = new X(20); System.out.println(xobj_1.m + " " + xobj_1.getn()); //20 100 X xobj_2 = new X( 40 ); System.out.println(xobj_2.m + " " + xobj_2.getn()); //40 100 X.setn( 1000 ); //(E) System.out.println(xobj_1.m + " " + xobj.1.getn()); //20 1000 System.out.println(xobj_2.m + " " + xobj_2.getn()); //40 1000 } }

Because the static member is private in line (A), we had to add to the class the getn and the setn methods, both static, in lines (B) and (C). Instead of accessing the static member n directly, we now do so through getn, which may be invoked directly on the class as shown in line (D), or on the objects made from the class, as in the two statements after line (D). Similarly, to change the value of the static member n, we invoke setn directly on the class, as in line (E). We could also invoke setn on the objects made from the class.

To illustrate the use of static members in more meaningful situations, we will now consider the same two examples—the Date class and the Robot class—we presented for the case of C++ in the previous section. For each, we will point out the similarities and differences between the Java and the C++ programs.

Example 1

We will consider first the Date class. Here is a comparable Java definition of the Date class:

 
//Date.java class Date { private int d, m, y; private static Date today = new Date( 31, 10, 2001 ); //(A) public Date( int dd, int mm, int yy ) { //(B) d = dd; m = mm; y = yy; } public Date( int dd, int mm ) { //(C) d = dd; m = mm; y = today.y; } public Date(int dd) { //(D) d = dd; m = today.m; y = today.y; } public Date() { //(E) d = today.d; m = today.m; y = today.y; } public static void setToday( int dd, int mm, int yy ) { //(F) today = new Date( dd, mm, yy ); } public void print() { System.out.println("day: " + d + " month: " + m + " year: " + y); } public static void main(String[] args) { Date d1 = new Date( 3, 1, 1970 ); d1.print(); // day: 1 month: 1 year: 1970 Date d2 = new Date( 2 ); d2.print(); // day: 2 month: 10 year: 2001 setToday( 3, 4, 2000 ); //(G) today.print(); // day: 3 month: 4 year: 2000 Date d3 = new Date(7); d3.print(); // day: 7 month: 4 year: 2000 Date d4 = new Date(); d4.print(); // day: 3 month: 4 year: 2000 } }

Note how the static member today is declared and initialized at the same time at (A), something that cannot be done in C++. (A static data member must also be const for C++ to allow for its in-class initialization.)

Additionally, for the C++ implementation of the Date class in the Date.cc program of Section 11.3, we used the mechanism of default arguments in a single three-argument constructor to simulate a two-argument constructor, a one-argument constructor, and a noargument constructor. Java does not permit default arguments for method parameters. So if we want these other constructors in addition to a three argument constructor in line (B), we have to provide those explicitly. That's what has been done in lines (C), (D), and (E) of the program.

Line (F) of the program above defines a static method for resetting the value of the static data member today. Line (G) of main invokes this method directly on the class to change the value stored in today. The code that follows line (G) demonstrates the results achieved with the constructors that set the state of the object partly according to what is stored in today.

Example 2

As was the case with the C++ program Robot.cc of the previous section, we will now show that a static data member can be modified by a nonstatic method to give a class useful functionality. The following program, with a static data member and no static methods at all (except, of course, for main), is the Java version of Robot.cc:

 
//Robot.java class Robot { public int idNum; public static int nextIdNum = 1; //(A) public String owner; public int getIdNum() { return idNum; } public String getOwner() { return owner; } public Robot() { idNum = nextIdNum++; }; //(B) public Robot(String name) { this(); owner = name; } //(C) public void print() { System.out.println(idNum + " " + owner); } public static void main( String[] args ) { Robot r1 = new Robot( "ariel" ); r1.print(); // 1 ariel Robot r2 = new Robot("mauriel"); r2.print(); // 2 maurial Robot r3 = new Robot("mercurial"); r3.print(); // 3 mercurial } }

The static member nextIdNum is declared at (A) and also initialized right there. This class member will keep track of the next ID number available for the next robot. The purpose of the no-arg constructor at (B) is to assign a value to the idNum member of a new Robot and, at the same time, to increment the value of the static member nextIdNum. The regular constructor calls the no-arg constructor as its first executable statement.




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