Java Interview Questions about the static keyword

What is the static keyword for?

The static keyword means that a field or a method belongs to a class and not to an instance of the class. So it can be accessed without the need for an instance of it’s containing class.

Even if there are multiple instances of a class, the static fields will have only one instance shared between all of the class instances.

Can a class be static?

No, you can’t use the static keyword for a top-level class definition, it only has a meaning for members inside a class.

Think about it, what would it mean for a class to be static? In java, a static element means that you can access/invoke it without an instance of the enclosing class; what that could possibly mean if you apply the keyword to the class itself?

However, you can use the static keyword for inner classes. This way you can create an inner class that can have an instance without requiring an instance of the parent class.

What is a static inner class?

It is a class defined inside another class, using the static modifier. It is not tied to an instance of the outer class, you can reference it on it’s own even from outside of the class by prefixing it with the outer class’s name.

Example of such a class (Address):

public class Person {

    static class Address {
        private String city;
        private String street;

        public Address(String city, String street) {
            this.city = city;
            this.street = street;
        }
    }
}

And it’s usage from outside of the class:

new Person.Address("Budapest", "Futo street");

Of course, you can also create an instance of it inside the class. In that case, you do not need to use the Person prefix.

Can you override a static method?

No, you can not override a private method in Java, if you create a similar method with same return type and same method arguments in descendant class then it will hide the superclass method, this is known as method hiding.

Is there a way for a static method of a class to access an instance variable of an instance of the same class?

The first guess could be for many people that it is not possible. This is true in most cases, because inside a static method you are not inside an instance of the containing class.

However, your method can take parameters. If you can pass the instance that you need to access as a parameter, you will be able to access it’s fields and methods that are visible.

When are static fields initialized?

Static fields are initialized at the time of class loading in Java, opposite to instance variable which is initialized when you create instance of a particular class.

What is the initialization order of static fields?

Static fields are initialized based on the textual order they appear in the code.

What risks can you think of regarding static fields and parallel running?

In a concurrent environment you need to take extra care to ensure that static fields won’t get in an unwanted state because of multiple threads are modifying it at the same time.

Of course, this issue can be present with non static fields as well, but static fields can be more easily accessed from various parts of a program.

A great way to prevent this issue is to make them final, so their value cannot change.

What are the disadvantages of a static method in Java?

You cannot override a static method in Java. This makes testing harder as you can’t replace it with a mock.

However there are ways to overcome this limitation if you really want to mock some static methods:

  • Use a tool like PowerMock.
  • Create a wrapper class around the static class and inject the wrapper. You can then mock it as well. But in this case it is wise to think it through if you really need to have your methods as static.

What is a static initializer block?

The static initializer block is a block of code that gets executed when the class is loaded by the JVM. This happens the first time it is referenced in the code.

Example:

public class MyClass {

    private static String computedComplexValue;

    static {
        // Imagine a complicated computation here.
        String firstName = "John";
        String lastName = "Doe";
        computedComplexValue = firstName + lastName;
    }
}

Are static initializer blocks thread safe?

Yes they are. The is no issue when multiple threads are trying to load the same class and run it’s static init block. It will only be loaded and executed once.

Multiple executions are only possible if you have multiple classloaders that load the class.

What is the execution order for static initializer blocks?

If you define multiple static blocks in a class, they will execute in the order they are define.

When you have multiple static blocks in you inheritance hierarchy, they will execute in order from super classes to child classes.