Search This Blog

Wednesday 13 August 2014

Initialize Static Class Object - JAVA

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