Skip to content

Static methods and classes

Static methods

A static method is a method that does not require an object to be created. Most often static methods are used if we want to execute the same algorithm in many places within the project.

Declaration

To declare a static method before the returned type , simply add the keyword static, such as:

public class MyPrinter {
    static void printNumber(int number) {
        System.out.println("The number is: " + number);
    }
}

Calling

To call the static method, we don't need to create an instance of the object, but simply refer directly to the class, e.g:

public class MyPrinterExample {
    public static void main(String[] args) {
        MyPrinter.printNumber(10); // The number is: 10
    }
}

It should be remembered that the abuse of static methods is a bad idea that will result in negative consequences very quickly. Firstly, we don't write objective code at this point, which is in denial of the logic of using object language. Secondly, this way of writing code doesn't allow us to use the potential of so-called Object Oriented Programming (OOP).

Static internal classes

What's an internal class?

The easiest way to illustrate it is with an example:

public class MyOuterClass {
    private int outerNumber = 5;

    public class MyInnerClass {
        public void printNumber() {
            System.out.println(outerNumber);
        }
    }

    public MyInnerClass init() {
        return new MyInnerClass(); // = this.new MyInnerClass()
    }
}

In the above example, we are dealing with an external class called MyOuterClass and an internal class called MyInnerClass defined in MyOuterClass. Access modifiers used before the definition of an internal class work identically to attributes, methods or constructors. It should also be added that an internal class has access to all fields and methods of the external class (also private) in which it was defined.

Creating an internal class instance

To create an internal class instance we need an external class instance. This is illustrated by an example:

public static void main(String[] args) {
    MyOuterClass myOuterClass = new MyOuterClass();
    MyOuterClass.MyInnerClass myInnerClass1 = myOuterClass.init();
    myInnerClass1.printNumber(); // 5
    MyOuterClass.MyInnerClass myInnerClass2 = myOuterClass.new MyInnerClass();
    myInnerClass2.printNumber(); // 5
}

To refer to the MyOuterClass.MyInnerClass type is nothing more than to refer to the public internal type. In the example above, we have created instances of an internal class in two ways. The myInnerClass1 instance is created by calling the init() method on an instance of an external class myOuterClass - this method in turn calls the constructor of an internal class new in MyInnerClass(). We also create the myInnerClass2 instance using the myOuterClass instance, but here we openly call the internal class constructor, that is: myOuterClass.new MyInnerClass().

What is a static internal class?

Let's start with a simple example:

public class MyOuterClass {
    public static class MyInnerClass {
        // declarations
    }

    public MyInnerClass init() {
        return new MyInnerClass();
    }
}

As can be seen in the example above, the only difference from a normal internal class is that the internal class declaration is preceded by a static modifier. It tells us that we are dealing with a declaration of static internal class. By default, all internal interfaces and enumeration types are static, the static modifier is redundant. What is important is the difference in creating an internal class static instance.

Creating an internal class static instance

Unlike the internal class, which is not static, you do not need an external class instance to create a static internal class instance. This is illustrated by an example:

public static void main(String[] args) {
    MyOuterClass myOuterClass = new MyOuterClass();
    MyOuterClass.MyInnerClass myInnerClass = new MyOuterClass.MyInnerClass();
    MyOuterClass.MyInnerClass myInnerClass1 = myOuterClass.init();
}

We can see that it is enough to call the constructor using the static internal class type new in MyOuterClass.MyInnerClass() to create its instance.

When to use static internal classes?

If there is a reason why we want to have an internal class and simultaneously instance regardless of its external class, then it must be a static class.

Static fields

Declaration

Static fields are a class attribute, not an object. This means that it can be referenced without creating an object instance. To declare a static field, simply add a static modifier before declaring the type:

public class StaticFieldExample {
    public int myNumber = 10; // normal public field, requires a class instance
    public static int myStaticNumber = 15; // class static field, NOT requiring a class instance
}

The myStaticNumber field will exist even if we do not create any instance.

Using static fields

Reference to a normal and static field is shown in the example below:

System.out.println(StaticFieldExample.myStaticNumber); // (1)
System.out.println(StaticFieldExample.myNumber); // Compilation error - attempt to refer to a non-static field!
StaticFieldExample staticFieldExample = new StaticFieldExample();
System.out.println(staticFieldExample.myNumber); // (2)

In the example above, in the line (1) we referred to the myStaticNumber variable based on the class type, not the instance of the object of that class. Attempting to refer to a non-static field myNumber without an object instance will result in a compilation error. The (2) example is a valid reference to an instance of the myNumber field.