Java Programs  «Prev 

Java Static Class Members

The static identifier is used to denote class members that are static. You can access a static class member without actually creating an object based on the class. Examples of static methods include methods defined in the standard Math class, which can be called without creating an instance of the class. Following is an example of calling a static method in the Math class to calculate the square root of a number:
Math.sqrt(1244.0);

A static method is only capable of calling other static methods. So, if you create methods in an application class and you want to call them from the main() method, you must declare the methods as static.
The same rule applies to accessing member variables from a static method; you can only access static member variables from a static method. Like static methods, static member variables can be accessed without creating an instance of a class.

Static Binding

Question: Which characteristics do 1) static methods and 2) static variables have in Java?
In Java, static methods and static variables share the following characteristics:
  1. They belong to the class rather than an instance of the class. This means that they are shared among all instances of the class.
  2. They can be accessed without creating an instance of the class. This means that they can be called directly from the class itself.

However, static methods and static variables also have some differences:
  1. Static methods cannot access non-static instance variables or methods. This is because static methods are not associated with any instance of the class.
  2. Static variables are initialized only once when the class is loaded into memory. This means that all instances of the class share the same value of the static variable.
  3. Static methods are often used for utility functions that don't need to access instance-specific data. Examples of static methods include Math.max(), String.valueOf(), and Arrays.sort().
  4. Static variables are often used to define constants or to maintain state that is shared among all instances of the class. Examples of static variables include MAX_VALUE in the Integer class and out in the System class

Static does not require an instance

You know by now that they do not require or depend on instances of the class. Polymorphism applies to instances rather than to classes, so static methods can be bound to their implementations at compile time. This is referred to as static binding or compile-time binding. Java restricts static methods from being overridden. However, remember from other parts of this website that they can be overloaded.
Overloaded methods differ only in the number and type of parameters, as the name is the same. To account for this possibility, the compiler will check the method signature and parameter types of static methods to find the appropriate implementation at compilation.

Polymorphic method invocations apply only to instance methods. You can always refer to an object with a more general reference variable type (a superclass or interface), but at runtime, the ONLY things that are dynamically selected based on the actual object (rather than the reference type) are instance methods. Not static methods. Not variables. Only overridden instance methods are dynamically invoked based on the real object's type.