简体   繁体   English

是否有默认的构造函数,即使我在课堂上提到了构造函数

[英]is there default constructor even if i mention constructor in class

public class Ex
{ 
  int a;   

  public Ex()   
  {
    System.out.println("a is "+a);   
  } 
}

output is:a is 0 输出为:a为0

where a gets initialized... 初始化的地方...

i know that default values for int is zero..my question is that where it gets initialied ..through default constructor ?(i heard that default constructor is created when we don't mention any constructor in the class) 我知道int的默认值为零..我的问题是它是通过默认构造函数初始化的。(我听说当我们在类中没有提到任何构造函数时会创建默认构造函数)

No, there is no default constructor when you write a specific on. 不,当您编写特定的on时,没有默认的构造函数。 But fields get initialized before any constructor is called. 但是,在调用任何构造函数之前,必须先初始化字段。 After initialization of fields initializers ({.. some code .. } blocks)are run and finally the constructor is executed. 字段初始化后,将运行初始化程序({.. some code ..}块),并最终执行构造函数。

The reason why a has an initial value is written in the Java language specification (4.12.5) : a具有初始值的原因写在Java语言规范(4.12.5)中

Each class variable, instance variable, or array component is initialized with a default value when it is created 每个类变量,实例变量或数组组件在创建时均使用默认值初始化

a is an instance variable (a non static field) and so it has an initial value. a是实例变量(非静态字段),因此具有初始值。 The value itself is specified too: 值本身也被指定:

For type int, the default value is zero, that is, 0. 对于int类型,默认值为零,即0。

It may be interesting to know that this is different for local variables (variables declared in a method body): 知道局部变量(在方法主体中声明的变量)有所不同可能很有趣:

A local variable must be explicitly given a value before it is used, by either initialization or assignment, in a way that can be verified by the compiler using the rules for definite assignment. 在使用局部变量之前,必须通过初始化或赋值为局部变量显式地赋予一个值,这种方式可以由编译器使用确定赋值规则进行验证。

So if you read a local variable that has not been initialized or "set" in your code yet, the compiler will give an error. 因此,如果您读取的局部变量尚未在代码中初始化或“设置”,则编译器将给出错误消息。

To clear your head, if you had not declared a zero-argument constructor and your class had no constructor(s), java creates a default zero-argument constructor for you. 为了澄清您的想法,如果您没有声明零参数构造函数,并且您的类没有构造函数,则Java为您创建一个默认的零参数构造函数。

As for your primitive types, once declared, its initialized (if uninitialized) with default values before use. 对于原始类型,一旦声明,则在使用前使用默认值对其进行初始化(如果未初始化)。

§ JLS - 8.8.9 Default Constructor §JLS-8.8.9默认构造函数

In your code you have supplied constructor if you haven't then . 如果没有,则在代码中提供了构造函数。

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. 如果一个类不包含构造函数 声明,则将自动提供一个不带参数的默认构造函数:如果所声明的类是原始类Object,则默认构造函数的主体为空。 Otherwise, the default constructor takes no parameters and simply invokes the superclass constructor with no arguments 否则,默认构造函数不使用任何参数,而仅调用不带参数的超类构造函数

在调用构造函数之前初始化基元

it occurs when Java allocates memory for a class. 当Java为类分配内存时,会发生这种情况。 It fils all fields with default values; 它将使用默认值过滤所有字段;

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

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