What will be the result of compiling and running the following program?
publicclass Main {
staticint a;
int b;/*fromwww.java2s.com*/public Main() {
int c;
c = a;
a++;
b += c;
}
publicstaticvoid main(String[] args) {
new Main();
}
}
Select the one correct answer.
(a) The code will fail to compile, since the constructor is trying to access the static members.
(b) The code will fail to compile, since the constructor is trying to use the static field a before it has been initialized.
(c) The code will fail to compile, since the constructor is trying to use the field b before it has been initialized.
(d) The code will fail to compile, since the constructor is trying to use the local variable c before it has been initialized.
(e) The code will compile and run without any problems.
(e)
Note
The fact that a field is static does not mean that it is not accessible from non-static methods and constructors.
All fields are assigned a default value if no initializer is supplied.
Local variables must be explicitly initialized before use.