aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Referencing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Referencing" Watch "Referencing" New topic
Author

Referencing

Wena Dollison
Greenhorn

Joined: Jul 03, 2002
Posts: 7
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

Joined: Aug 26, 2001
Posts: 7610
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Jun 19, 2002
Posts: 110
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??


Amir
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Jul 03, 2002
Posts: 7
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Referencing