Mic*_*dan 71 java initialization global-variables local-variables default-value
根据我的参考,基本类型具有默认值,对象为空.我测试了一段代码.
public class Main {
public static void main(String[] args) {
int a;
System.out.println(a);
}
}
Run Code Online (Sandbox Code Playgroud)
该行将System.out.println(a);
是指向变量的错误a
,variable a might not have been initialized
而在给定的引用中,该变量integer
将具有0
默认值.但是,使用下面给出的代码,它实际上会打印出来0
.
public class Main {
static int a;
public static void main(String[] args) {
System.out.println(a);
}
}
Run Code Online (Sandbox Code Playgroud)
第一个代码可能出现什么问题?类实例变量的行为是否与局部变量不同?
Jun*_*san 65
在第一个代码示例中,a
是一个main
方法局部变量.方法局部变量在使用之前需要初始化.
在第二个代码示例中,a
是类成员变量,因此它将初始化为默认值.
Pao*_*olo 62
仔细阅读您的参考:
默认值
声明字段时并不总是需要分配值. 声明但未初始化的字段将由编译器设置为合理的默认值.一般来说,此默认值将为零或null,具体取决于数据类型.然而,依赖于这样的默认值通常被认为是糟糕的编程风格.
下表总结了上述数据类型的默认值.
...
局部变量略有不同; 编译器永远不会为未初始化的局部变量分配默认值.如果无法初始化声明它的局部变量,请确保在尝试使用它之前为其赋值.访问未初始化的局部变量将导致编译时错误.
Kos*_*ias 14
这些是涉及的主要因素:
注1:您必须在每个实现的构造函数上初始化最终成员变量!
注意2:您必须在构造函数本身的块内初始化最终成员变量,而不是调用另一个初始化它们的方法.例如,这是无效的:
private final int memberVar;
public Foo() {
//invalid initialization of a final member
init();
}
private void init() {
memberVar = 10;
}
Run Code Online (Sandbox Code Playgroud)
注3:数组是Java中的对象,即使它们存储基元.
注意4:初始化数组时,其所有项都设置为默认值,与成员或本地数组无关.
我附上一个代码示例,介绍上述情况:
public class Foo {
//static and member variables are initialized to default values
//primitives
private int a; //default 0
private static int b; //default 0
//objects
private Object c; //default NULL
private static Object d; //default NULL
//arrays (Note: they are objects too, even if they store primitives)
private int[] e; //default NULL
private static int[] f; //default NULL
//what if declared as final?
//primitives
private final int g; //not initialized, MUST set in constructor
private final static int h; //not initialized, MUST set in a static {}
//objects
private final Object i; //not initialized, MUST set in constructor
private final static Object j; //not initialized, MUST set in a static {}
//arrays
private final int[] k; //not initialized, MUST set in constructor
private final static int[] l; //not initialized, MUST set in a static {}
//initialize final statics
static {
h = 5;
j = new Object();
l = new int[5]; //elements of l are initialized to 0
}
//initialize final member variables
public Foo() {
g = 10;
i = new Object();
k = new int[10]; //elements of k are initialized to 0
}
//A second example constructor
//you have to initialize final member variables to every constructor!
public Foo(boolean aBoolean) {
g = 15;
i = new Object();
k = new int[15]; //elements of k are initialized to 0
}
public static void main(String[] args) {
//local variables are not initialized
int m; //not initialized
Object n; //not initialized
int[] o; //not initialized
//we must initialize them before usage
m = 20;
n = new Object();
o = new int[20]; //elements of o are initialized to 0
}
}
Run Code Online (Sandbox Code Playgroud)
在声明原始类型值时,需要记住一些事项.
他们是:
所以在你的代码中:
public class Main {
int instanceVariable;
static int staticVariable;
public static void main(String[] args) {
Main mainInstance = new Main()
int localVariable;
int localVariableTwo = 2;
System.out.println(mainInstance.instanceVariable);
System.out.println(staticVariable);
// System.out.println(localVariable); //will throw compilation error
System.out.println(localVariableTwo);
}
}
Run Code Online (Sandbox Code Playgroud)