class Size{
Size(){
}
//public final Size SMALL = new Size();//infinite loop
final Size as = new Size();//infinite loop
//static Size aw = new Size();//No infinte loop
}
class b{
static public void main(String ftr[]){
Size z = new Size();
}
}
when static Size aw is equal to new Size(); there is error when above program is run.
See the scenario below, when the program is run with:
public final Size SMALL = new Size();// infinite loop is happening
final Size as = new Size();
an infinite loop is happening and program is giving
Exception in thread "main" java.lang.StackOverflowError
at Size.<init>(Interesting.java:6)
at Size.<init>(Interesting.java:6)
Can somebody explain to me or, point me to the places where I can find the reason behind this.
Because
asin line 6 is an instance attribute ( part of the object ) so you can read it like this:Hence inifinite loop.
In the ohter hand when you mark it with the
staticqualifier, you make it a class attribute ( part of the class not of the object ) so you can read it like.Then Java says… “ok, got it, I’ll do with then the time comes”. The class definition is created and when loaded a new instance of that class is created. No confusion nor infinite loop arise.
I hope this helps.