I have a few cases I wonder about. First, if you have no constructor:
class NoCons { int x; }
When I do new NoCons()
, the default constructor gets called. What does it do exactly? Does it set x
to 0, or does that happen elsewhere?
What if I have this situation:
class NoCons2 extends NoCons { int y; }
What happens when I call new NoCons2()
? Does NoCons
's default constructor get called, and then NoCons2
's constructor? Do they each set the respective x
and y
fields to 0?
What about this version:
class Cons2 extends NoCons { int y; public Cons2() {} }
Now I have a constructor, but it doesn't call the super class's constructor. How does x
ever get initialized? What if I had this situation:
class Cons { int x; public Cons() {} }
class NoCons2 extends Cons { int y; }
Will the Cons
constructor be called?
I could just try all these examples, but I can't tell when a default constructor is run. What's a general way to think about this so that I'll know what happens in future situations?