Creating an instance within the Class itself
Posted
by
didxga
on Stack Overflow
See other posts from Stack Overflow
or by didxga
Published on 2012-06-25T15:10:44Z
Indexed on
2012/06/25
15:15 UTC
Read the original article
Hit count: 166
java
|instantiation
What's going on when the assignment statement executed at Line 4
, does compiler ignore the new operator
and keep the foo
variable being null or something else happen to handle this awkward moment?
public class Foo {
// creating an instance before its constructor has been invoked, suppose the "initializing"
// gets printed in constructor as a result of the next line, of course it will not print it
private Foo foo = new Foo();//Line 4
public Foo() {
System.out.println("initializing");
}
}
© Stack Overflow or respective owner