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

pc-build windows motherboard January 29, 2022 How to fix blank screen when entering BIOS in MSI MAG X570 Tomahawk Motherboard?

If you’re using a CPU that doesn’t have an integrated graphics module (like AMD Ryzen 7 5800X that I am using), you have to install an external GPU in order to get your output on a monitor/screen. Ergo, I had to use one of my old NVIDIA GTX 1080 graphics card in my PC.

gadget unboxing monitor September 3, 2020 Unboxing of my new LG Ultragear 27GN950-B monitor

I ordered the LG Ultragear 27GN950 from Amazon.de. I compared all the Amazon EU websites and it was the cheapest in Germany. When I ordered it showed that it would deliver in 1-2 months but luckily it came back in stock in a day or two and was dispatched right after. I got the monitor delivered to Ireland within 4-5 days.

review unboxing gadget November 20, 2020 Unboxing and Review of UE Megablast

I got the UE Megablast from Amazon as it was on a Prime deal. Normally, it costs around €160-€220 in Europe but I got it for €110 because of this offer. This is the most expensive Bluetooth speaker from UE. In this blog post, I would do the unboxing as well as give my reviews as I’ve been using it for around two months now.