This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Referencing  RSS feed

 
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!
 
Gold Digger
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
 
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
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.
 
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
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!
 
Have you no shame? Have you no decency? Have you no tiny ad?
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!