Java默认构造函数
到底什么是默认构造函数 - 您能告诉我以下哪一个是默认构造函数以及它与任何其他构造函数的区别吗?
public Module() {
this.name = "";
this.credits = 0;
this.hours = 0;
}
public Module(String name, int credits, int hours) {
this.name = name;
this.credits = credits;
this.hours = hours;
}
What exactly is a default constructor — can you tell me which one of the following is a default constructor and what differentiates it from any other constructor?
public Module() {
this.name = "";
this.credits = 0;
this.hours = 0;
}
public Module(String name, int credits, int hours) {
this.name = name;
this.credits = credits;
this.hours = hours;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
他们都不是。如果您定义了它,则它不是默认值。
默认构造函数是自动生成的无参构造函数,除非您定义另一个构造函数。任何未初始化的字段都将设置为其默认值。对于您的示例,假设类型为
String
、int
和int
,并且类本身是公共的,则看起来像这样:与 完全一样
与完全没有构造函数完全一样。但是,如果您至少定义了一个构造函数,则不会生成默认构造函数。
请参阅 Java 规范,特别是:Java 规范部分。 oracle.com/javase/specs/jls/se22/html/jls-8.html#jls-8.8.9" rel="nofollow noreferrer">8.8.9。 Java 语言规范的默认构造函数。
说明
从技术上讲,默认初始化字段的不是构造函数(默认或其他)。然而,我留下了答案,因为
Neither of them. If you define it, it's not the default.
The default constructor is the no-argument constructor automatically generated unless you define another constructor. Any uninitialised fields will be set to their default values. For your example, it would look like this assuming that the types are
String
,int
andint
, and that the class itself is public:This is exactly the same as
And exactly the same as having no constructors at all. However, if you define at least one constructor, the default constructor is not generated.
See the Java specifications, specifically: Section 8.8.9. Default Constructor of Java Language Specification.
Clarification
Technically it is not the constructor (default or otherwise) that default-initialises the fields. However, I am leaving it the answer because
如果您未在类中定义任何构造函数,则会创建默认构造函数。它只是一个不执行任何操作的无参数构造函数。编辑:除了调用
super()
。A default constructor is created if you don't define any constructors in your class. It simply is a no argument constructor which does nothing. Edit: Except calling
super()
.如果您未在类中显式定义至少一个构造函数,则编译器会自动生成默认构造函数。您已经定义了两个,因此您的类没有默认构造函数。
根据 Java 语言规范第三版:
A default constructor is automatically generated by the compiler if you do not explicitly define at least one constructor in your class. You've defined two, so your class does not have a default constructor.
Per The Java Language Specification Third Edition:
你好。据我所知,让我澄清一下默认构造函数的概念:
我从 Java 教程 中阅读了此信息。
Hi. As per my knowledge let me clear the concept of default constructor:
I read this information from the Java Tutorials.
Java 提供了一个默认构造函数,当未提供显式构造函数时,该构造函数不接受任何参数,也不执行任何特殊操作或初始化。
隐式默认构造函数执行的唯一操作是使用 super() 调用来调用超类构造函数。构造函数参数为您提供了一种为对象初始化提供参数的方法。
下面是包含 2 个构造函数的多维数据集类的示例。 (一个默认构造函数和一个参数化构造函数)。
Java provides a default constructor which takes no arguments and performs no special actions or initializations, when no explicit constructors are provided.
The only action taken by the implicit default constructor is to call the superclass constructor using the super() call. Constructor arguments provide you with a way to provide parameters for the initialization of an object.
Below is an example of a cube class containing 2 constructors. (one default and one parameterized constructor).
通用术语是,如果您没有在对象中提供任何构造函数,则会自动放置一个无参数构造函数,称为默认构造函数。
如果您确实定义了一个与以下情况下将放置的构造函数相同的构造函数:你不提供任何它通常被称为无参数构造函数。只是一个约定,尽管有些程序员更喜欢将这个显式定义的无参数构造函数称为默认构造函数。但是,如果我们明确定义一个名称,则不会使其成为默认值。
根据文档
如果一个类不包含构造函数声明,则隐式声明一个没有形式参数且没有 throws 子句的默认构造函数。
示例
将自动修改(通过添加默认构造函数)如下
,当您创建它时object
调用此默认构造函数。
General terminology is that if you don't provide any constructor in your object a no argument constructor is automatically placed which is called default constructor.
If you do define a constructor same as the one which would be placed if you don't provide any it is generally termed as no arguments constructor.Just a convention though as some programmer prefer to call this explicitly defined no arguments constructor as default constructor. But if we go by naming if we are explicitly defining one than it does not make it default.
As per the docs
If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.
Example
will automatically be modified(by adding default constructor) as follows
and when you create it's object
this default constructor is invoked.
默认构造函数是指在没有任何程序员定义的构造函数的情况下由编译器自动生成的构造函数。
如果程序员没有提供构造函数,编译器会隐式声明一个默认构造函数,该构造函数调用
super()
,没有 throws 子句,也没有形式参数。例如
default constructor refers to a constructor that is automatically generated by the compiler in the absence of any programmer-defined constructors.
If there's no constructor provided by programmer, the compiler implicitly declares a default constructor which calls
super()
, has no throws clause as well no formal parameters.E.g.
如果一个类没有程序员提供的任何构造函数,那么java编译器将添加一个不带参数的默认构造函数,该构造函数将通过super()调用在内部调用超类构造函数。这称为默认构造函数。
在您的情况下,当您以编程方式添加它们时,没有默认构造函数。
如果您没有添加构造函数,那么编译器生成的默认构造函数将如下所示。
注意:在默认构造函数中,它还会添加 super() 调用,以调用超类构造函数。
添加默认构造函数的目的:
构造函数的职责是初始化实例变量,如果没有实例变量,您可以选择从类中删除构造函数。但是,当您继承某个类时,您的类有责任调用超类构造函数以确保超类正确初始化其所有实例变量。
这就是为什么如果没有构造函数,java编译器将添加一个默认构造函数并调用超类构造函数。
If a class doesn't have any constructor provided by programmer, then java compiler will add a default constructor with out parameters which will call super class constructor internally with super() call. This is called as default constructor.
In your case, there is no default constructor as you are adding them programmatically.
If there are no constructors added by you, then compiler generated default constructor will look like this.
Note: In side default constructor, it will add super() call also, to call super class constructor.
Purpose of adding default constructor:
Constructor's duty is to initialize instance variables, if there are no instance variables you could choose to remove constructor from your class. But when you are inheriting some class it is your class responsibility to call super class constructor to make sure that super class initializes all its instance variables properly.
That's why if there are no constructors, java compiler will add a default constructor and calls super class constructor.
当我们没有显式地为类定义构造函数时,java就会为该类创建一个默认构造函数。它本质上是一个非参数化构造函数,即它不接受任何参数。
默认构造函数的工作是调用超类构造函数并初始化所有实例变量。如果超类构造函数不存在,那么它会自动将实例变量初始化为零。因此,这就是使用构造函数的目的,即初始化对象的内部状态,以便创建实例的代码将拥有一个完全初始化的可用对象。
一旦我们为类定义了自己的构造函数,就不再使用默认构造函数。因此,它们实际上都不是默认构造函数。
When we do not explicitly define a constructor for a class, then java creates a default constructor for the class. It is essentially a non-parameterized constructor, i.e. it doesn't accept any arguments.
The default constructor's job is to call the super class constructor and initialize all instance variables. If the super class constructor is not present then it automatically initializes the instance variables to zero. So, that serves the purpose of using constructor, which is to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object.
Once we define our own constructor for the class, the default constructor is no longer used. So, neither of them is actually a default constructor.
当您没有在类中定义任何构造函数时,编译器会为您定义默认构造函数,但是当您声明任何构造函数时(在您的示例中您已经定义了参数化构造函数),编译器不会为您执行此操作。
由于您已在类代码中定义了构造函数,因此编译器不会创建默认构造函数。创建对象时,您正在调用默认对象,该对象在类代码中不存在。然后代码就报编译错误。
When you don’t define any constructor in your class, compiler defines default one for you, however when you declare any constructor (in your example you have already defined a parameterized constructor), compiler doesn’t do it for you.
Since you have defined a constructor in class code, compiler didn’t create default one. While creating object you are invoking default one, which doesn’t exist in class code. Then the code gives an compilation error.
当你创建一个新的 Module 对象时,java 编译器会为你添加一个默认的构造函数,因为根本没有构造函数。
如果您添加任何类型的构造函数(甚至是非参数构造函数),那么您将拥有自己的 java 构造函数,并且不再添加默认构造函数。
这是一个无参数构造函数,即使您没有父类,它也会在内部调用 super() 构造函数。 (如果您的类没有父类,则将调用 Object.Class 构造函数)
When you create a new Module object, java compiler add a default constructor for you because there is no constructor at all.
If you add any kind of constructor even and non-arg one, than java thing you have your own and don't add a default constructor anymore.
This is an non-arg constructor that internelly call the super() constructor from his parent class even you don't have one. (if your class don't have a parent class than Object.Class constructor will be call)
默认构造函数不带任何参数:
A default constructor does not take any arguments: