简体   繁体   English

为什么默认构造函数需要在实例化Object时在具有Parameterized Constructor的POJO文件中声明?

[英]Why Default constructor need to declare in POJO file which has Parameterized Constructor while instantiating Object?

Suppose I have one POJO class User with a constuctor public User(int id, String name){...} . 假设我有一个POJO类User,其中包含一个constuctor public User(int id, String name){...} But when I instantiate the User object like User u=new User() with no parameter Eclipse gives error like The constructor User() is undefined . 但是当我实例化User对象之类的User u=new User()时没有参数Eclipse会给出错误,如构造函数User()未定义 But it works fine when I have no parameterized Constructor. 但是当我没有参数化的构造函数时,它工作正常。 Can someone please explain why It requires to define default constructor? 有人可以解释为什么它需要定义默认构造函数?

The default (no-parameter) constructor is ONLY provided if you have provided no others. 如果您未提供其他构造函数,则仅提供默认(无参数)构造函数。 If you define even a single constructor for your class, you MUST use one of the explicitly defined (ie, in your code) constructors to instantiate the object. 如果为类定义了一个构造函数,则必须使用显式定义的(即在代码中)构造函数之一来实例化对象。 You can, of course, define your own zero-parameter, empty constructor if that works for what you're trying to do. 当然,您可以定义自己的零参数空构造函数,如果它适用于您要执行的操作。

Edit: Answer of why? 编辑:回答why?

The compiler provides a default constructor so that the Object can be Instantiated when there are no constructors defined. 编译器提供默认构造函数,以便在没有定义构造函数时可以实例化Object。 But if you have defined a parametric constructor, it means that when you create a new instance of that class, its variables should initialized with the parameters you have passed(or do something similar). 但是,如果您已经定义了参数化构造函数,则意味着当您创建该类的新实例时,其变量应使用您传递的参数进行初始化(或执行类似的操作)。 Without those initializations, the object might not behave in an expected way. 如果没有这些初始化,对象可能不会以预期的方式运行。 Hence the compiler prevents such things from happening by not defining a default constructor(when you have defined one). 因此,编译器通过不定义默认构造函数(当您定义了默认构造函数时)来防止发生这种情况。

The no-arg constructor will be automatically added by the compiler if no constructor is provided by the developer. 如果开发人员没有提供构造函数,编译器将自动添加no-arg构造函数。 However, as soon as you put your own custom parameterized constructor, the compiler stops adding default constructor for you. 但是,只要您放置自己的自定义参数化构造函数,编译器就会停止为您添加默认构造函数。

In this scenario, if you still want to use your no-arg constructor, you have to provide it yourself explicitly: 在这种情况下,如果您仍想使用no-arg构造函数,则必须自己提供它:

public User() {
}

public User(int id, String name) {
}

The logic behind this is that: if you define your own parametrized constructor, you are declaring that the parameters listed in the constructor is required to construct an object of the class. 这背后的逻辑是:如果定义自己的参数化构造函数,则声明构造函数中列出的参数是构造类的对象所必需的。 Therefore you are also implicitly declares if the user of your library do not provide these two parameters, the object shouldn't be able to construct. 因此,如果库的用户不提供这两个参数,您也可以隐式声明,该对象不应该构造。 Thus the compiler will not add the no-arg constructor for you. 因此编译器不会为您添加no-arg构造函数。

If you want to also declare that your class can still work if none of the specified parameters in the parametrized constructor is provided and you (no arg), then you have the explicitly declare that by providing the non-arg constructor yourself. 如果你想声明你的类仍然可以工作,如果参数化构造函数中没有提供任何指定的参数而你(没有arg),那么你可以通过自己提供非arg构造函数来明确声明它。

I am giving answer so late, but let's try to share with you what i know: 我这么晚才回答,但让我们试着和你分享我所知道的:

  1. When you don't provide constructor compiler provides constructor. 当你不提供构造函数编译器提供构造函数时。 Why ? 为什么? Because it is sure you are going to initialize your object with no argument constructor only. 因为您确定要仅使用参数构造函数初始化对象。 So compiler does it for you. 所以编译器会为你做。
  2. When you provide parameterised constructor, then compiler doesn't know which constructor you will use to initialize your object. 当您提供参数化构造函数时,编译器不知道您将使用哪个构造函数来初始化对象。 So compiler does not provide for you one no-argument constructor. 所以编译器没有为你提供一个无参构造函数。 So you have to write explicitly. 所以你必须明确写。

    Hope it will help you. 希望它会对你有所帮助。

Java compiler automatically provides a no-parameter, default constructor for any class without constructors. Java编译器自动为没有构造函数的任何类提供无参数的默认构造函数。 If there is no constructor defined in your class then Java compiler will add a no parameter constructor in your generated class file. 如果您的类中没有定义构造函数,那么Java编译器将在生成的类文件中添加无参数构造函数。 But if there is a constructor with parameter in your class, then you need to write the no-parameter constructor, compiler will not add it. 但是如果你的类中有一个带参数的构造函数,那么你需要编写无参数构造函数,编译器不会添加它。

The compiler automatically provides a no-argument, default constructor for any class without constructors but if you explicitly provide any constructor with arguments then compiler will not provide a default constructor mainly due to security reasons. 编译器自动为没有构造函数的任何类提供无参数的默认构造函数,但如果您明确地为任何构造函数提供参数,那么编译器将不会提供默认构造函数,主要是出于安全原因。

So what you can do is 所以你能做的就是

 public User(int id, String name){...}
 public User(){this(defualtID,defaultName)};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM