Can you make Static Constructor in Java?


Java does not permit to declare a constructor as static. Following are the reasons.

1. Static means for the same class. For example, static methods cannot be inherited.

2. With static, "this" reference (keyword) cannot be used. "this" is always linked to an object. A constructor always belongs to some object.

3. If a constructor is static, an object of subclass cannot access. If static is allowed with constructor, it is accessible within the class but not by subclass.

The following program raises compilation error.

public class Demo
{
  public static Demo()
  {
    System.out.println("Hello 1");
  }
  public static void main(String args[])
  {
    Demo d1 = new Demo();
  }
}


Static Constructor
Output screen on Static Constructor

Observe, the compiler error message. It says, static is not allowed with constructor.

What other access modifiers a constructor cannot be?

"A constructor cannot be abstract, static, final, native, strictfp, or synchronized". Explained clearly in Java Constructor Properties

Pass your comments and suggestions on this tutorial "Static Constructor".

Are you thorough with arrays? Then read this: Converting Numbers to Words

  1. Clear your confusion on Constructors: Java Constructors: Learn through Questions and Answers
  2. Can you convert in between Binary and Octal: int to Binary, Octal, Hexa

4 thoughts on “Can you make Static Constructor in Java?”

  1. First, the key word static means that everything marked static must be the class-level thing and belongs to the class only.While constructors belong to object and they may usually be called when we use the **new** operator.So we now know that a constructor is not even a class property,how could we possibly mark it as static?

    Second,static constructor violates the whole purpose of inheritance in java.Every time just before we create an subclass object ,JVM automatically calls the superclass constructor to make it ready for the subclass object to be created.But if we mark the constructor static,the subclass will not be able to access the constructor of its superclass because it’s marked static thus belongs to class only.

  2. I know static methods are not overridden.but i dont understand the sentence that static methods cannot be inherited in your blog.pls clear the doubt?

  3. Yes abstract class have constructor.
    abstract class A
    {
    A()
    {
    System.out.println(“Hello from constructor”);
    }
    void f1();
    }
    class Demo extends A
    {
    public void f1()
    {
    System.out.println(“method f1()”);
    }
    public static void main(String[] args)
    {
    A ob=new Demo();
    ob.f1();
    System.out.println(“Hello World!”);
    }
    }

    Interface can be declared as abstract.

    abstract interface A1
    {
    void f1();
    }
    class A implements A1
    {
    public void f1()
    {
    System.out.println(“method f1()”);
    }
    public static void main(String[] args)
    {
    A ob=new A();
    ob.f1();
    System.out.println(“Hello World!”);
    }
    }

Leave a Comment

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