nanode
02-23-2001, 12:20 AM
Today a co-worker approached me and shared a strange technique some people use in their java code.
I can write it better than I can explain verbally:
public class FxCnx {
int a;
int b;
public FxCnx() {
System.out.println("" + a + " " + b);
}
{
a = 1;
System.out.println("Bull**** constructor 1");
}
{
b = 2;
System.out.println("Bull**** constructor 2");
}
public static void main(String[] args) {
System.out.println("main");
FxCnx x = new FxCnx();
}
}
Apparently the plain { } blocks get called before the constructor.
Check the console:
nanode@stout:~/myjava$ java FxCnx
main
Bull**** constructor 1
Bull**** constructor 2
1 2
nanode@stout:~/myjava$
Can someone fill me in?
I can write it better than I can explain verbally:
public class FxCnx {
int a;
int b;
public FxCnx() {
System.out.println("" + a + " " + b);
}
{
a = 1;
System.out.println("Bull**** constructor 1");
}
{
b = 2;
System.out.println("Bull**** constructor 2");
}
public static void main(String[] args) {
System.out.println("main");
FxCnx x = new FxCnx();
}
}
Apparently the plain { } blocks get called before the constructor.
Check the console:
nanode@stout:~/myjava$ java FxCnx
main
Bull**** constructor 1
Bull**** constructor 2
1 2
nanode@stout:~/myjava$
Can someone fill me in?