Never confuse education with intelligence, you can have a PhD and still be an idiot.
- Richard Feynman -

Java:Language - Static or non-static

From Juneday education
Jump to: navigation, search


A staic method is a method which is relevant without considerations of instances of a class. Static methods are similar in the same way.

Math.max(double a, double b)

A method which is static does not belong to any specific object. The only information and data it needs comes from its arguments (and/or from static variables).

In contrast to an instance method, a static method can be called even before any instances of the class it is declared in has been created. A static method is "resolved" in compile time, whereas instance methods are resolved dynamically in runtime. You cannot "override" a static method, they are always resolved by the class name and therefore there is no question of which method is to be considered.

That is also why you always should call a static method via its class name (or directly if it's statically imported), to avoid confusion which otherwise would come from the call looking like a call to an instance method:

// Don't:
Integer i = Integer.valueOf(13);
String binary = i.toBinaryString(i); // Looks like a call to an instance method - confusing

// Do:
Integer i = Integer.valueOf(13);
String binary = Integer.toBinaryString(i); // Clearly, toBinaryString is a static method

Static binding

A static method is resolved at compile-time, i.e. the definition is known at compile-time. Calling code is therefore tightly depending on a fixed definition.

Compare that to the instance method public String toString() which every object has, either by explicitly defining its own version or implicitly via inheritance from any super class or the top-level Object class.

When toString() is called (for instance as a result of calling println)the compiler need not know the run-time type of the object. At run-time, the first definition of toString() which is found is called. This is called dynamic binding.

Note: There is a static toString in java.util.Arrays as a convenient way of getting a String representation of an array. That is NOT the toString() we talk about here!

Independent of an instance

A static method in a class is independent of any instances of the class. You don’t have to create an instance of the class in order to call the static method. In fact, the static method can be called before any instances have been created. You call it using ClassName.staticMethodName(args). As a consequence, a static method has no access to any instance variables of the class. If it had, and if that were allowed, which instance should it choose in order to access the instance variable?

Remember: instance variables are unique to every created instance of the class. The instances all get their own copy of all the instance variables. An instance variable only exists as a part of an instantiated object.

Data a static method can operate on

Since a static method can’t access any instance variables in the class it belongs to, where does it get it’s data to operate on?

A static method can access static variables, its formal parameters and any local variables it creates. Parameters:

public static int max(int a, int b) {
 if (a > b) {
   return a;
 return b;

Here's an example of a static method accessing a static variable:
<source lang="Java">
public class MyUtil {
  public final static int BATTLE_OF_HASTINGS=1066;
  public static int yearsSinceHastings(int year){
    return year-BATTLE_OF_HASTINGS;

Client code:

int year = 2015;
System.out.println("It’s been " +
                   MyUtil.yearsSinceHastings(year) +
                   " years since the battle of Hastings.");

When to use

A typical use for a static method is a utility method that can be called stand-alone without any instance attached to it, like the methods in Math for calculating values and the sort methods in Arrays and Collections.

In most cases seen from an object oriented perspective, however, you create objects which have methods to operate on the values the objects hold as their state. Such methods, which deal with the state of the objects, must be instance methods, since only instance methods can access the instance variables of the objects.

If your method deals with the instance variables of your object, it must be an instance method, i.e. non-static. If your method doesn’t care about any specific object and its state in order to be called, it is typically a static method.

Examples of static methods in the Java API

Some examples of static methods include methods for sorting lists and arrays, making an array into a string, and various conversion methods. Let's look at some code:

String[] strs = {"Lemon", "Banana", "Orange"};
// We don’t need an instance of Arrays in order to ask it to sort an array
// which we provide as an argument. Same with turning an array into a String

for (int i = 0; i < 16; i++) {
  System.out.println(Integer.toBinaryString(i) +
                     " (" +
                     Integer.toHexString(i) +
// We don’t need an instance of Integer to ask the class to convert an int to some kind of string...

Other examples include getting a system property:

System.out.println(System.getProperty("") +
                   " " +
System.out.println(System.getProperty("user.dir")); // current directory

The System, Arrays and Math classes even cannot be instantiated. How did they do that? Hint: private!


Lecture slides

Lecture slides for the Static or non-static lecture. A short repetition for those who have forgotten when to use static and when not to, and what's the difference, anyway?

Video lecture

Further reading

Where to go next

To be decided.