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.
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.
- public class NoStaticConstructor {
- public static NoStaticConstructor() {
- System.out.println("default");
- }
- public static void main(String[] args) {
- NoStaticConstructor st = new NoStaticConstructor();
- }
- }
No comments:
Post a Comment