so far I have seen two approaches of setting a variable’s value in Java. Sometimes a constructor with arguments is used, others setter methods are used to set the value of each variable. I know that a constructor initialises an instance variable inside a class once a class is instantiated using the “new” Keyword. But when do we use constructors
Tag: constructor
Initialize field before super constructor runs?
In Java, is there any way to initialize a field before the super constructor runs? Even the ugliest hacks I can come up with are rejected by the compiler: Note: The issue disappeared when I switched from inheritance to delegation, but I would still like to know. Answer No, there is no way to do this. According to the language
Circular dependency in Java constructors
I have the following classes. and As can be clearly seen, there is a circular dependency between the classes. if I try to run class A, I eventually get a StackOverflowError. If a dependency graph is created, where nodes are classes, then this dependency can be easily identified (at least for graphs with few nodes). Then why does the JVM