Java Static ConstructorLast Updated : 17 Mar 2025 Constructors are unique methods in Java that initialize objects. When a class instance is created, they are called. A constructor lacks a return type and shares the same name as the class. Constructors in Java can have parameters or not (default constructors are parameterless). 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:
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. Explanation In 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. The provided code begins with the declaration of a class named StaticConstructorDemo. This class serves as a basic template in which the constructor and the main method will be defined. Class declarations in Java are straightforward, using the public keyword to make the class accessible from other classes. However, this declaration is not valid in Java. Constructors in Java cannot be static because they are meant to initialize individual instances of the class. The static keyword signifies that a method or variable belongs to the class itself, not to any particular instance. Consequently, combining static with a constructor, which inherently deals with instance initialization, results in a contradiction. Java does not allow static constructors, and attempting to compile this code will lead to a compilation error. 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 cannot be inherited from their subclasses because they belong to the class in which they are declared. Similarly, we cannot 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 cannot 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 static, it cannot be accessed by its subclasses and will belong to a class level only. The program will not be compiled and will throw a compile-time error. StaticConstructorExample.java StaticConstrutorChild.java: Output: In StaticConstructorExample Class In StaticConstructorChild class Method of StaticConstructorChild class Explanation In the above example, we note that when the child class object is created, it first invokes the parent class constructor and then its own class constructor. It is happening because the new keyword creates the object and then invokes the constructor to initialize 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 cannot create a static constructor in Java. ConclusionJava constructors cannot be static because doing so prevents the constructor from being called from the child class. This prevents the creation of the child class's object and causes a compile-time error. This approach upholds the integrity of object-oriented principles by making sure that every instance of a class is initialized correctly along its constructor chain. Furthermore, class-level initialization is accomplished by static methods and blocks, which are distinct from instance creation. It enables the correct configuration of static variables and other class-wide settings. Writing reliable Java applications that effectively use inheritance, encapsulation, and polymorphism requires an understanding of these differences. Java supports maintainable and scalable code by following these guidelines, which guarantee that all objects are initialized in a consistent and predictable manner. Next TopicFractional-knapsack-problem-in-java |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India