What are Static Methods on interface in Java?

In Java SE 8 and later, you can define static methods on interfaces. A static method is a method associated with the class, not the instance. This means you can call a static method without creating an instance of the class.

This feature can be particularly useful when providing utility methods that act on instances of the interface. You would normally keep these in a separate utility class, but by having these on the interface itself can lead to more readable and maintainable code.

Here is a simple example:

interface MyInterface {
    static void myStaticMethod() {
        System.out.println("Static Method on Interface");
    }
}

public class Main {
    public static void main(String[] args) {
        MyInterface.myStaticMethod(); // Call without creating instance
    }
}

In this example, myStaticMethod() is a static method defined on MyInterface. You call it using the interface name (MyInterface.myStaticMethod()), without needing to create an instance of MyInterface.

Keep in mind that static methods in interfaces are not inherited by classes that implement the interface or sub-interfaces, so you always have to use the interface name when calling them.

The Stream interface in Java has several static methods that provide useful functionality for working with sequences of elements, such as collections. Here is an example that uses the Stream.of() static method, which allows you to create a Stream from a set of objects:

import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        Stream.of("Hello", "World", "Interface", "Static", "Methods")
              .map(String::toUpperCase)
              .forEach(System.out::println);
    }
}

In this example, we use Stream.of() to create a Stream from a set of String objects. We then use map() to convert each string in the stream to uppercase, and forEach() to print out each string.

Here is another example, this time using the IntStream.range() static method:

import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        IntStream.range(1, 6)
                 .forEach(System.out::println);
    }
}

In this example, IntStream.range(1, 6) creates a stream of integers from 1 (inclusive) to 6 (exclusive). The forEach() method is then used to print out each integer in the stream.

How do I create static variables in Java?

Class variables or static variables are variables that are declared with static modifier. A given class will have only one copy of each of its static variables, regardless of how many times the class has been instantiated.

If the value of a static variable is changed, the new value is available equally in all instances of the class. The final keyword could be added to indicate the value of static variable will never change.

If you try to assign a new value to final variable, you will get a compile error.

package org.kodejava.basic;

public class StaticDemo {
    // static variable with final value that never change
    final static int Y = 20;
    // static variable
    static int x = 12;
    // non-static variable
    int z;

    public static void main(String[] args) {
        StaticDemo sd0 = new StaticDemo();

        System.out.println("x before update = " + StaticDemo.x);
        System.out.println("y= " + StaticDemo.Y);

        sd0.z = StaticDemo.x + StaticDemo.Y;
        System.out.println("z= " + sd0.z);

        StaticDemo.x = 15;
        System.out.println("x after update = " + StaticDemo.x);

        StaticDemo sd1 = new StaticDemo();
        StaticDemo sd2 = new StaticDemo();
        StaticDemo.x = 20;

        System.out.println("StaticDemo.x = " + StaticDemo.x);
        System.out.println("sd0 = " + sd0.getX());
        System.out.println("sd1 = " + sd1.getX());
        System.out.println("sd2 = " + sd2.getX());

        //
        // try to assign value to final variable, it will cause a
        // compile time error
        //
        // StaticDemo.Y = 30;
    }

    public int getX() {
        return StaticDemo.x;
    }
}

Here is the output printed by the program:

x before update = 12
y= 20
z= 32
x after update = 15
StaticDemo.x = 20
sd0 = 20
sd1 = 20
sd2 = 20

How do I use static import feature?

In order to use a static member of a class in Java we have to qualify the reference with the class name where they came from. For instance to access the PI and abs() from the Math class we should write:

double circle = Math.PI * 10;
int absolute = Math.abs(-100);

For some time you might want to call the members without the class name. This is allowed in Java 5.0 by using a feature called static import. It’s an import statement that allows you to statically import static class member. A static import declaration enables you to refer to imported static members as if they were declared in the class that uses them, the class name and a dot (.) are not required to use an imported static member.

You can write something like the following to static import.

import static java.lang.Math.PI;
import static java.lang.Math.*;

For a clear code it is better to import each member separately and not using the “*” to import every static member in your code.

Let’s see a simple static import below:

package org.kodejava.basic;

import java.util.Date;

import static java.lang.Math.PI;
import static java.lang.Math.abs;
import static java.lang.System.out;

public class StaticImport {
    public static void main(String[] args) {
        // Using static field PI and static method abs() from the
        // java.lang.Math class.
        double circle = PI * 10;
        int absolute = abs(-100);

        // Using static field of the java.lang.System class to
        // print out the current date.
        out.println("Today: " + new Date());
    }
}