Can constructors be marked final, abstract or static in Java?


Except public, protected and, private constructor does not allow any other modifier.

  • When you use a final keyword with a method or constructor it cannot be overridden. But, a constructor in Java cannot be overridden therefore, there is no need of using the final keyword with the constructor.

  • Since you cannot override a constructor you cannot provide body to it if it is made abstract. Therefore, you cannot use abstract keyword with the constructor.

  • If you want to invoke a member of a class before instantiating the class you need to use static before it. But, contractors are called implicitly at the time of instantiation. Therefore, there is no point in using static with constructors.

Example

public class Sample {
   public final static Sample(){
      System.out.println("Hello");
   }
   public static void main(String args[]) throws Exception{
      Sample obj = new Sample();
   }
}

Output

C:\Sample>javac Sample.java
Sample.java:2: error: modifier static, final not allowed here
public final static Sample(){
                    ^
1 error

Lakshmi Srinivas
Lakshmi Srinivas

Programmer / Analyst / Technician

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements