Saturday, April 27, 2019

Can we have static constructor in java?

Problem statement:
Can we have static constructor in java?

No ! we cannot have static constructor in java. The purpose of constructor is to initialise the object of the class. So there is no meaning to make constructor as static and thus compiler gives an error at compile time saying modifier 'static' not allowed.
  1. public class NoStaticConstructor {
  2.     public static NoStaticConstructor() {
  3.         System.out.println("default");
  4.     }

  5.     public static void main(String[] args) {
  6.         NoStaticConstructor st = new NoStaticConstructor();
  7.     }
  8. }
Output: compile time error - modifier 'static' is not allowed

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...