Using default Constructors in java, even if the parameterized constructors are present [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-06 15:54:07

问题


I just wanted to clear my concept here, so i am asking...

If I define an explicit parameterized constructor for my class, can I still invoke the default constructor provided by the java compiler (which is provided for every class by default) ??

Or does it cause result in a compile time error in such case?? Please explain what exactly happens with respect to the calls made by the compiler !!


回答1:


If and only if no constructor is provided, then a no-argument constructor is created by the compiler.

The JLS states in Chapter 8:

If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.

The one "catch" is:

It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor (§6.6) that takes no arguments and has no throws clause.




回答2:


8.8.9 Default Constructor

If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided:

  • If the class being declared is the primordial class Object, then the default constructor has an empty body.
  • Otherwise, the default constructor takes no parameters and simply invokes the superclass constructor with no arguments

.




回答3:


Java Language Specification can be surprisingly helpful and actually easy to read.




回答4:


  • For each an every class which haven't any constructor java will automatically added default constructor - which haven't no arguments. And no args super call.
  • If you add at lease one constructor to the class, then java will not include the default constructor.
  • As a result of that it will case a compilation error when you try to use no args constructor.

In addition to that this can lead some extra compilation errors. Test this code.

class A{
  A(int i){}
}

class B extends A{}

class Test{
  PSVM{
    B b = new B();
  }
}

This code gives a compilation error.

Class B haven't any constructor. So java will put something like this as default constructor.

B(){
  super();
}

But Supper class of class A (class B) haven't constructor that takes no args.So it guves an compilation error.

So it is better to have a no argument constructor in each class you are writing.




回答5:


if you just define the parameterized constructor then compiler will not provide default constructor

and it will print an error




回答6:


Java provides a default no-argument constructor only for those classes which don't have a constructor explicitly defined for them. Once a constructor is defined by the programmer (even if it is a no-argument constructor), the default constructor is not provided.

Now you were asking about error:

  • If you have defined a no-argument constructor for your class, then a no-argument invocation of a constructor of your class will invoke this constructor and no compiler error will be thrown
  • if you have only defined constructor(s) which accept arguments then the no-argument invocation of your class' constructor will raise an compiler error coz no no-argument constructor will be present.

Would suggest a thorough reading of Java Language Specification




回答7:


If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.

If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.

It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor that takes no arguments and has no throws clause.

In a class type, if the class is declared public, then the default constructor is implicitly given the access modifier public; if the class is declared protected, then the default constructor is implicitly given the access modifier protected; if the class is declared private, then the default constructor is implicitly given the access modifier private; otherwise, the default constructor has the default access implied by no access modifier.



来源:https://stackoverflow.com/questions/10827346/using-default-constructors-in-java-even-if-the-parameterized-constructors-are-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!