Marc B
2013-11-18 22:42:06 UTC
Hello,
Preparing for the Java exam I have a question about static initializers.
A static initializer block can be used to initialize instance and static variables (http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html). So I tried it out and it ended up with this:
//============================================================
package LearnJava;
public class Holder{
// initialize instance varialbe
{
int int1 = 5;
System.out.println("Initializing instance variable int1: " + int1);
}
// initialize static variable
static {
int sint1 = 6;
System.out.println("Initializing static block. sint1: " + sint1);
}
// initialize instance varialbe
{
int int2 = 7;
System.out.println("Initializing instance variable int2: " + int2);
}
// initialize static varialbe
static {
int sint2 = 8;
System.out.println("Initializing static variable sint2: " + sint2);
}
int int1 = 5;
public Holder(){
}
public static void main(String[] args){
Holder h = new Holder();
//System.out.println("Running main. sint1: " + Holder.sint1);
System.out.println("Running main. int1: " + h.int1);
}
}
//============================================================
I expect to be able to refer to int1, int2 and sint1 and sint2 from an instantiated class and - in the case of the static variables - from the class directly. But this class does not compile because of line
System.out.println("Running main. sint1: " + Holder.sint1).
Also System.out.println("Running main. sint1: " + h.sint1) doesn't compile. The error is:
error: cannot find symbol
System.out.println("Running main. sint1: " + Holder.sint1);
^
symbol: variable sint1
location: class Holder
1 error
If I comment out this line it compiles and I get output
Initializing static block. sint1: 6
Initializing static variable sint2: 8
Initializing instance variable int1: 5
Initializing instance variable int2: 7
Running main. int1: 5
Q:
Why is the instance variable in the static {} block suddenly unknown? And why can the instance variable declared in {} be resolved???
Does this mean the static variable scan only be initialized outside any block?
Related: When I put static int sint1 = 6; n ieither the constructor or main() I get an Netbeans error "illegal start of expression" (the line gets underlined red and hovering over displays the error). Running te program gives a dialog "no main classes found"
Thanks,
Marc
Preparing for the Java exam I have a question about static initializers.
A static initializer block can be used to initialize instance and static variables (http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html). So I tried it out and it ended up with this:
//============================================================
package LearnJava;
public class Holder{
// initialize instance varialbe
{
int int1 = 5;
System.out.println("Initializing instance variable int1: " + int1);
}
// initialize static variable
static {
int sint1 = 6;
System.out.println("Initializing static block. sint1: " + sint1);
}
// initialize instance varialbe
{
int int2 = 7;
System.out.println("Initializing instance variable int2: " + int2);
}
// initialize static varialbe
static {
int sint2 = 8;
System.out.println("Initializing static variable sint2: " + sint2);
}
int int1 = 5;
public Holder(){
}
public static void main(String[] args){
Holder h = new Holder();
//System.out.println("Running main. sint1: " + Holder.sint1);
System.out.println("Running main. int1: " + h.int1);
}
}
//============================================================
I expect to be able to refer to int1, int2 and sint1 and sint2 from an instantiated class and - in the case of the static variables - from the class directly. But this class does not compile because of line
System.out.println("Running main. sint1: " + Holder.sint1).
Also System.out.println("Running main. sint1: " + h.sint1) doesn't compile. The error is:
error: cannot find symbol
System.out.println("Running main. sint1: " + Holder.sint1);
^
symbol: variable sint1
location: class Holder
1 error
If I comment out this line it compiles and I get output
Initializing static block. sint1: 6
Initializing static variable sint2: 8
Initializing instance variable int1: 5
Initializing instance variable int2: 7
Running main. int1: 5
Q:
Why is the instance variable in the static {} block suddenly unknown? And why can the instance variable declared in {} be resolved???
Does this mean the static variable scan only be initialized outside any block?
Related: When I put static int sint1 = 6; n ieither the constructor or main() I get an Netbeans error "illegal start of expression" (the line gets underlined red and hovering over displays the error). Running te program gives a dialog "no main classes found"
Thanks,
Marc