Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Referencing

 
Wena Dollison
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've encountered this while experimenting codes, but still wondering about the error.

1. class Referencing {
2. int y = 1;
3.
4. Referencing() {
5. this(y);
6. }
7.
8. Referencing(int x) {
9. y=x++ + ++x;
10. }
11.
12. public static void main(String [] args) {
13. Referencing r = new Referencing();
14. System.out.println(r.y);
15. }
17.}

Please explain why line 5 gives you a compile-error: cannot reference y before supertype constructor has been called

Thanks!
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

When the second constructor is invoked on line 2, "this" has no existence, thus "this.y" is not legal.
As a complement, please read the following:
JLS 8.8.5.1 Explicit Constructor Invocations
JLS 12.5 Creation of New Class Instances
 
Alan Chong
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();" ...
 
Wena Dollison
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic