Home » Java Static Constructor

Java Static Constructor

by Online Tutorials Library

Java Static Constructor

In Java, a constructor is not allowed to be abstract, final, static, native, or strictfp. So, there is no static constructor in Java.

A static constructor used to initialize static data means the specified task will execute only once throughout the program. Usually, a static constructor is automatically called when the first instance is generated, or any static member is referenced. The static constructor is explicitly declared by using a static keyword. However, the static constructor is not allowed in Java.

Some key features of the static constructor are as follows:

  • It will not take parameters or access modifiers.
  • A specific class can have only one static constructor.
  • It does not allow inheritance or overloading.
  • It is invoked automatically, can not be called directly or explicitly.
  • If the value of static fields is not initialized, it will initialize to default values.

Let’s understand why the static constructor is not allowed in Java:

What if We Declare a Static Constructor?

A static constructor is not allowed in Java programming. It is illegal and against the Java standards to use a static constructor. So, the Java program will not be compiled and throw a compile-time error.

Let’s understand it with an example. Consider the below example:

StaticConstructorDemo.java:

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:   Illegal modifier for the constructor in type StaticConstructorDemo; only public, protected & private are permitted.    at StaticConstructorDemo.(StaticConstructorDemo.java:4)  at StaticConstructorDemo.main(StaticConstructorDemo.java:13)  

From the above example, we can see, it is throwing an exception “Illegal modifier for the constructor type“. And it is clearly written that public, protected, & private are permitted.

Why does Java not Support a Static Constructor?

When we mark anything with a static keyword, it belongs to class only, for example, static method, static variable, etc. Static methods can not be inherited from their subclasses because they belong to the class in which they are declared. Similarly, we can not use a static variable in its subclasses.

In the case of a constructor, a constructor is a reusable block of code, which means we can call it from its subclasses during the creation of the objects. But, when we declare it as static, it can not be used by its subclasses other than the declaring classes. So, it is illegal to declare a constructor as static. Thus, it will violate the whole motive of the inheritance concept.

If we declare a constructor as static, then it can not be accessed by its subclasses and will belong to a class level only. The program will not be compiled and throw a compile-time error.

Let’s understand it with an example:

StaticConstructorExample.java:

StaticConstrutorChild.java:

Output:

In StaticConstructorExample Class  In StaticConstructorChild class  Method of StaticConstructorChild class  

From the above example, we can notice that when the child class object is created, first it invokes the parent class constructor, then its own class constructor. It is happening because the new keyword is creating the object then invoking the constructor for initializing the values. Every child class has a super() constructor as the first statement to inherit the properties from the parent class.

This is the reason why we can not create a static constructor in Java.

Conclusion:

The constructors in Java can not be static because if the constructors are marked as static, they can not be called from the child class; thus, the child class’s object will not be created. The program will not be compiled and throw a compile-time error.


You may also like