Why Java Static Constructor is not allowed?

Filed Under: Java

Java Static Constructor is not allowed, but why? Before we dig into the reasons for not allowing static constructor, let’s see what happens if we want to make a constructor static.

Java Static Constructor

Let’s say we have a class defined as:


public class Data {

	private int id;
	
	public static Data() {}
}

If you will try to compile this class, you will get an error message as Illegal modifier for the constructor in type Data; only public, protected & private are permitted.

Java Static Constructor

Why Static Constructor is not allowed?

Let’s see some of the reasons that make the compelling arguments for not allowing static constructor in java.

Static Belongs to Class, Constructor to Object

We know that static methods, block or variables belong to the class. Whereas a Constructor belongs to the object and called when we use the new operator to create an instance. Since a constructor is not class property, it makes sense that it’s not allowed to be static.

Static Block/Method can’t access non-static variables

We know that static methods can’t access non-static variables. Same is true for static block also.

Now, the main purpose of a constructor is to initialize the object variables. So if we make constructor as static then it won’t be able to initialize the object variables. That will defeat the whole purpose of having a constructor for creating the object. So it is justified to have the constructor as non-static.

Notice that we can’t use this inside a static method to refer to the object variable. Below code will give compilation error as: Cannot use this in a static context.


public static void main(String args[]) {
	System.out.println(this.id);
}

java static method this error

Static Constructor will break inheritance

In Java, every class implicitly extends Object class. We can define a class hierarchy where subclass constructor calls the superclass constructor. This is done by super() method call. Most of the times JVM automatically calls the superclass constructor but sometimes we have to manually call them if there are multiple constructors in the superclass.

Let’s see an example for super() usage.


package com.journaldev.util;

class Data {
	Data() {
		System.out.println("Data Constructor");
	}
}

public class DataChild extends Data{
	public DataChild() {
		super(); //JRE calls it explicitly, calling here for explanation
		System.out.println("DataChild Constructor");
	}
	
	public static void main(String args[]) {
		DataChild dc = new DataChild();
	}
}

Above program will produce the following output.


Data Constructor
DataChild Constructor

If you look at the super() method, it’s not static. So if the constructor becomes static, we won’t be able to use it and that will break inheritance in java.

Java Static Constructor Alternative

If you want to initialize some static variables in the class, you can use static block. Note that we can’t pass arguments to the static block, so if you want to initialize static variables then you can do that in the normal constructor too.


class Data {
	public static int count;
	
	static {
		count = 0;
	}
	Data(int c) {
		//not recommended since the count is class variable 
		//and shared among all the objects of the class
		count=c; 
	}
}

Summary

Java static constructor is not allowed and we have very good reasons for that. We can initialize static variables using the static block as well as through constructor itself.

Comments

  1. Mr K says:

    I have to disagree with most of the statements made by this article. I’ll go in order:

    2.1 Static Belongs to Class, Constructor to Object
    The author’s argument is that a constructor belongs to an object, therefore you can’t have constructor in static context. This is like saying a code block belongs to a function therefore you can’t have static blocks. There are no such rules. The term “constructor” is not reserved for object creation. When you have a static field initializer, you are “constructing” the static scope of your class. I don’t really see any difference other than how we call them between a static constructor or a static block. Take a look at C#.

    2.2 Static Block/Method can’t access non-static variables
    True, there is no “this” in static context, because there is no object. Similarly again to static field initializers. Again, one could argue the same way: initializers are for object fields, therefore static fields make no sense… The purpose of a static constructor would be to initialize the static fields (hopefully).

    2.3 Static Constructor will break inheritance
    No, it will not. It doesn’t break it in C# and it doesn’t break it in the author’s static block example. It is the same thing. In case the base class would define a static constructor, the static context of the base class would be initialized before the inherited class’s static context is being initialized, similarly to a “super()” call, only… again… on static context, not object context. Hence the word static.

    This is a naming question, in other languages (like C#), static constructor behaves the same way as the static blocks in Java.

  2. Rani says:

    When static is for class then why can’t we have static class as we can have only static inner classes

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages