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 ();
}
}
In-Class Exercise 4.1: Download StaticExample.java and add a second data member (say, another int) to the 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; // Won't compile
printy (); // Won't compile
}
}
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):
Java allows the programmer to build on an existing
class using inheritance:
(source file) 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.
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 ();
}
}
public class StaticExample4 extends StaticExample {
// A new function
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.
}
}
(And StaticExample
is the superclass
or parent
class of
StaticExample4.
Instead of adding new data and methods, you can override a parent's methods and data in the subclass.
For example consider StaticExample5.java:
Note:
As we will see later, function overriding is a
key aspect of object-oriented programming.
public class StaticExample5 extends StaticExample {
// A new function 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 function in this class.
StaticExample.printx(); // The parent's function is still accessible.
}
}
public final class StaticExample {
// Rest of class not shown
}
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);
}
}
In-class exercise 4.2: Write code to test whether the following assertions are true: