Now at last we will understand what the static keyword means.
public class StaticExample { static double x; // Static data static void printx () // Static method { System.out.println ("x = " + x); } public static void main (String[] argv) { x = 5.34; printx (); } }
Exercise 4.1: In StaticExample.java add a second data member (say, another int) to StaticExample. Then, in main, assign a value to the new variable and print it.
What happens if you leave out the keyword static? E.g., (source file)
public class StaticExample2 { static double x; // Static data static void printx () // Static method { System.out.println ("x = " + x); } double y; // Non-static data void printy () // Non-static method { System.out.println ("y = " + y); } public static void main (String[] argv) { x = 5.34; printx (); y = 9.67; printy (); } }
Exercise 4.2: Try the above code in StaticExample2.java. What is the compiler error?
The problem is, both y and printy() are not static and need an object instance to be created (using new) before use.
The following does work (source file):
public class StaticExample3 { static double x; // Static data static void printx () // Static method { System.out.println ("x = " + x); } public double y; // Non-static data public void printy () // Non-static method { System.out.println ("y = " + y); } public static void main (String[] argv) { x = 5.34; printx (); // Create an instance of the object. StaticExample3 z = new StaticExample3 (); // Then, use it. z.y = 9.67; z.printy (); } }
Note:
Consider again the following simple static object (source file):
public class StaticExample { static double x; // Static data static void printx () // Static method { System.out.println ("x = " + x); } public static void main (String[] argv) { x = 5.34; printx (); } }
Java allows the programmer to build on an existing class using inheritance: (source file)
public class StaticExample4 extends StaticExample { // A new method static void printxNicely () { System.out.println ("The value of x is " + x); } public static void main (String[] argv) { x = 5.34; // The variable x is inherited. printx (); // This method is inherited. printxNicely (); // This is new. } }
Note:
Why is inheritance useful?
However, this form of inheritance is of limited use without the ability to override superclass methods and data in the subclass.
Instead of adding new data and methods, you can override a parent's methods and data in the subclass.
For example, consider StaticExample5.java:
public class StaticExample5 extends StaticExample { // A new method that overrides the parent's printx() static void printx () { System.out.println ("The value of x is " + x); } public static void main (String[] argv) { x = 5.34; // The variable x is inherited. printx (); // The printx method in this class. StaticExample.printx(); // The parent's method is still accessible. } }
Note:
public final class StaticExample { // ... Rest of class not shown ...
As we will see later, method overriding is a key aspect of object-oriented programming.
Let's look at an example: (source file)
class A { static int w = 1; private static int x = 2; public static int y = 3; protected static int z = 4; public static void main (String[] argv) { System.out.println (" w = " + w); System.out.println (" x = " + x); System.out.println (" y = " + y); System.out.println (" z = " + z); } } class B extends A { public static void main (String[] argv) { System.out.println (" w = " + w); System.out.println (" x = " + x); // Compiler error. System.out.println (" y = " + y); System.out.println (" z = " + z); } } class C { public static void main (String[] argv) { System.out.println (" w = " + A.w); System.out.println (" x = " + A.x); // Compiler error. System.out.println (" y = " + A.y); System.out.println (" z = " + A.z); } }
Exercise 4.3:
Write code to test whether
the following assertions are true: