实例变量初始化的位置 [英] where instance variables get initialized

查看:71
本文介绍了实例变量初始化的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

输出为:a是0

其中a初始化...

我知道int的默认值是零。我的问题是它得到的地方通过默认构造函数初始化?(我听说当我们没有在类中提及任何构造函数时创建默认构造函数)

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)

推荐答案

如果您没有自己初始化 a (它是原始值),它会自动初始化为默认值,在这种情况下为0。

If you didn't initalize a yourself (it's a primitive value), it gets initialized automatically to it's default value, 0 in this case.

阅读4.5.5节。 (此文档中的(变量的初始值) 。

Read section 4.5.5. (Initial Values of Variables) in this document.

这篇关于实例变量初始化的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆