Skip to content

Tag: constructor

Behavior of costructors in sub classes in Java

let’s say I have the following code: What are the outputs when I try to execute I thought it would first call the constructor of B and the constructor of B will call the constructor of A but the I don’t know which between “creating A” or “Hi” message will be shown first. The first thing invoked on a class

Setter methods or constructors

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

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