You're never calling the ObjectA()
constructor, except in the ObjectA
constructor. If you ever did call the constructor (e.g. from main
), you'd get a stack overflow because you'd be recursing forever.
It's not really clear what you're trying to do or why you're using a static variable, but your code would be simpler as:
public class ObjectA {
private String message = "The message";
public static void main(String[] args) {
ObjectA oa = new ObjectA();
System.out.println(oa.message);
}
}
Also note that the compiler never throws an exception. It's very important to distinguish between compile-time errors (syntax errors etc) and execution-time errors (typically exceptions).