Java中的默认初始化 [英] Default initialization in java

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

问题描述

我对Java中的变量初始化感到困惑.据我了解,类变量会默认初始化,而局部变量默认不会初始化.但是,如果我使用new关键字在方法内部创建一个数组,则默认情况下确实会对其进行初始化.所有对象都是这样吗?不管它是类变量还是局部变量,使用new关键字都会初始化对象吗?

I have a confusion about variable initialization in Java. As I understand, class variables get default initialization while local variables are not initialized by default. However, if I create an array inside a method using the new keyword, it does get initialized by default. Is this true of all objects? Does using the new keyword initialize an object regardless of whether it's a class variable or local variable?

推荐答案

所有对象都是这样吗?使用new关键字是否会初始化 对象,而不管它是类变量还是局部变量?

Is this true of all objects? Does using the new keyword initialize an object regardless of whether it's a class variable or local variable?

使用new关键字时.这意味着您已经初始化了您的对象.它是在方法级别还是在实例级别声明的,都没有关系.

When you use new keyword. it means that you have initialized your Object. doesn't matter if its declared at method level or instance level.

public void method(){
Object obj1;// not initialized
Object obj2 = new Object();//initialized
}

这篇关于Java中的默认初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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