Loading...

Statics in Java

java tech
Ram Patra Published on August 8, 2019
Image placeholder

Static is one of the non-access modifier which has a huge role in Java. It can be used with both variables and methods. It is used when we want some variable or method not to be a part of any specific object of a class rather common for all objects of the class.

For example, keeping count of all the instances created for a particular class we can make the count variable static or when we have a method which returns the same result irrespective of the instance invoking it like getRandom() returning any random number or getMax(int x, int y) returning the larger number between x and y we can make the methods static.

Ways to access static variables/methods

As static variable/methods doesn’t belong to any particular object of a class, you need not create any object to access static variable/method. You can access directly with the help of classname and dot '.' operator but you can also access normally like any other instance variable/method i.e, via object reference and dot '.' operator. See below:

class Frog {

    static int frogCount = 0; // Declare and initialize
                              // static variable
    int frogSize = 0;

    public Frog() {
        frogCount += 1;  // Modify the value in the constructor
    }

    public static int getFrogCount() {
        return frogCount;
    }
    
    public int getFrogSize() {
        return frogSize;
    }

    public static void main(String[] args) {
        Frog f = new Frog();
        System.out.println(f.getFrogSize()); // Access instance
                                             // method via object reference f
        
        System.out.println(Frog.frogCount);  // Access static
                                             // variable via class 
        
        System.out.println(f.frogCount);  // Access static
                                          // variable via object reference f
                                          
        System.out.println(Frog.getFrogCount());  // Access static
                                                  // method via class 
                
        System.out.println(f.getFrogCount());  // Access static
                                               // method via object reference f
    }
}

Some points to remember here

  • Static method cannot access a instance(non-static) variable directly.
public static int getFrogSize() {
    return frogSize; // compiler error
}

But they can access through a object reference.

public static int getFrogSize() {
    Frog obj = new Frog();
    return obj.frogSize; // ok
}
  • Static method cannot access a instance method directly.
public static int getFrogSizeSum() {
    int size = getFrogSize(); // compiler error
    return size * frogCount;
}

But they can access through a object reference.

public static int getFrogSizeSum() {
    Frog obj = new Frog();
    int size = obj.getFrogSize(); // ok
    return size * frogCount;
}
  • Static method can access a static variable or static method.

  • Static methods cannot be overridden. This is an important point. Consider the below program to understand:

class Animal {
    static void doStuff() {
        System.out.print("a ");
    }
}

class Dog extends Animal {
    // it's a redefinition,
    // not an override
    static void doStuff() {
        System.out.print("d ");
    }
    
    public static void main(String[] args) {
        Animal[] a = {new Animal(), new Dog(), new Animal()};
        for (int x = 0; x < a.length; x++)
            a[x].doStuff();
    }
}

The output of the above program is 'a a a'. If the doStuff() method have been overridden then the output would have been 'a d a'. Because in case of method overriding polymorphism comes to play, according to which the actual object type determines which method is to be invoked but in this case the object reference determines which method is to be invoked (doStuff() of Animal class is invoked all the time as the object reference is of Animal type) i.e, no overriding and no polymorphism.

In other words, it perfectly makes sense as only inherited methods are overridden and static methods are never inherited as they don’t belong to any object, so they can’t be overridden.

  • Static methods can be overloaded in the same class as well in the child classes. Read method overloading to be more clear.
public class Foo {
    public static void doStuff(int x) { }

    public static void doStuff(int x, int y) { }  // Valid overload as DIFFERENT ARGUMENT LIST
                                                  // (and methods can be overloaded
                                                  // in the same class or in subclass)
}

class Bar extends Foo {

    public static void doStuff(String x) { }  // Valid overload as DIFFERENT ARGUMENT LIST
                                              // (and methods can be overloaded
                                              // in the same class or in subclass)
}
Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on August 8, 2019
Image placeholder

Keep reading

If you liked this article, you may like these as well

November 29, 2019 How to add search to your Jekyll website

Jekyll is a static website generator that takes your plain text files and converts them to a beautiful website/blog.

gadget unboxing hifi November 5, 2021 Unboxing of KEF Q350 Speakers

I always wanted to have a nice music system in my house since I was a kid. However, I now finally got the chance to build my own HiFi audio system. After some research and listening to different speakers at my local HiFi stores, I shortlisted the following speakers based on my budget:

May 22, 2019 Access Control in Java

Modifiers fall into two categories: