Java中的本地变量和实例变量之间有什么区别 [英] what is the difference between local and instance variables in Java

查看:555
本文介绍了Java中的本地变量和实例变量之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了范围和存储差异之外,java中的实例和局部变量之间是否还有其他主要区别?

Except the scope and the storage differences, are there any other major difference between instance and local variables in java?

推荐答案

我能想到的另一件事是:

One extra thing I can think of:

实例变量被赋予默认值,如果是对象引用则为null,如果是int,则为0。

Instance variables are given default values, ie null if it's an object reference, 0 if it's an int.

局部变量没有得到默认值,因此需要显式初始化(​​如果你不这样做,编译器通常会抱怨)。

Local variables don't get default values, and therefore need to be explicitly initialized (and the compiler usually complains if you fail to do this).

这篇关于Java中的本地变量和实例变量之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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