The first thing a constructor does is either call its superclass constructor (using super()) or one of the other constructors (using this()). As long as the superclass constructor hasn't been called the object doesn't exist. In this case, "y" is an instance member, and thus, cannot be referenced because the instance has not been created yet. The constructor can be rewritten as:
A lot of Java's behaviors are just strange. This program clearly shows that until the superclass's constructor( Object in this case ) is called, no instance variables in subclass can be referenced. I've seen this anywhere. Can someone explain it please.
posted 16 years ago
A lot of Java's behaviors are just strange. strange? I'd say logical You cannot use a feature (instance variable y) of an object (i.e. instance of Referencing) that has not been created yet. In order for an object to be constructed, all its superclasses must have been initialized first. As long as superclasses have not been instantiated, the object does not exist. Please read the links I gave above and you will get it.
ok, I've slightly modified the above code to the one shown below. dose that mean that the second constructor (line 8) now has an implicit super() call to it's super class??
posted 16 years ago
dose that mean that the second constructor (line 8) now has an implicit super() call to it's super class?? That's correct. From JLS 8.8.5 Constructor Body
If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body is implicitly assumed by the compiler to begin with a superclass constructor invocation "super();" ...
Hi guys! I just replied due to super typhoon and there's still no electricity, argh! =) Just dropped by to say THANKS A LOT! You guys were really a great help! Thanks Valentin for the links! Keep it up!
Have you no shame? Have you no decency? Have you no tiny ad?
Programmatically Create PDF Using Free Spire.PDF with Java