This program shows how to initialize static object of a complex type (not primitive) in Java.
AnotherClass.java
public class AnotherClass {
public int x;
public int getX() {
return x;
}
}
MainClass.java
public class MainClass {
public static AnotherClass ac;
static{
System.out.println("static ...");
ac = new AnotherClass();
ac.x = 3;
}
public static void main(String[] args){
System.out.println(ac.getX());
}
}
No comments:
Post a Comment