Java Variable Types 变量类型
本文参考这里
data-type variable [ = value][, variable [= value] ...];
Local 变量
- declared in methods, constructors, or blocks
- 不能使用访问限定符(Access modifiers cannot be used for local variables)
- Local变量放在 stack 里,若没初始化则无默认值
Instance 变量
- declared in a class, but outside a method, constructor or any block.
- 实例变量在object被
new
时创建,在object被destroyed的时候销毁 - 可以使用访问限定符(Access modifiers can be given for instance variables.)
- 在class内,实例变量具有可见性(visible for all methods, constructors and block in the class)
- 实例变量有默认值(Instance variables have default values)
Class/static 变量
ClassName.VariableName
访问- 一个类不管创建多少objects,
static
变量仅有一个拷贝值 static
变量存储在static区(Static variables are stored in static memory)static
变量在程序开始时创建,程序停止时销毁- 常见地,
static
变量多被声明为public
- 常见地,
static
变量多同时用在final
变量上 - 默认值参照实例变量的
例:
public static final String DEPARTMENT = "Development";