A coworker (who is very new to Java) stopped in today and asked what seemed like a very simple question. Unfortunately, I did an absolutely horrible job of trying to explain
A class is loaded into memory by classloader and is initialized if any of the following happens.
1) an Instance of class is created using either new() keyword or using reflection using class.forName(), which may throw ClassNotFoundException in Java.
2) an static method of Class is invoked.
3) an static field of Class is assigned.
4) an static field of class is used which is not a constant variable.
5) if Class is a top level class and an assert statement lexically nested within class is executed.
so by line 1, class is both loaded and initialized and hence there is no problem in instantiating an instance of class in itself.
But if your code is like this,
class Test {
Test test2 = new Test();
public static void main(String[] args) {
Test test1 = new Test();
}
}
the above code will result in stackoverflow exception.
class Test {
public static void main(String[] args) {
Test test = new Test();
}
}
In the above code creating an instance won't call main again.
Remember, main is a static method, not tied to any particular instance.
class Test {
static Test test2 = new Test();
public static void main(String[] args) {
Test test1 = new Test();
}
}
This code will also run fine.
Read more from https://javarevisited.blogspot.in/2012/07/when-class-loading-initialization-java-example.html