• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compiler error

 
Puja S
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Why is this code giving a compiler error : cannot reference f before supertype constructor has been called ( cannot understand the error ) ?

public class Test19 {
float f;
Test19(){
this(f);
f = 3;
}
Test19(float f ) {
System.out.println(f);
}
public static void main(String args[]) {
Test19 t = new Test19();
}
}

Thanks .
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use any instance variables until the superclass constructor has been called.
If you call a constructor of your own class in your constructor (like you're doing) the superclass constructor does not get implicitly called in that constructor.
In your case you're passing an instance variable in that call which is illegal.
 
Anand Ko
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is illegal forward reference.
 
Richard Vagner
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to JLS:


An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.

[ January 07, 2005: Message edited by: Richard Vagner ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not clear ...
Can any body explain it in simple words ...

thanks .
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok: this(f); is illegal.
 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Jeroen ,
for your information ... this was not explanation ... understand .
 
amit taneja
Ranch Hand
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok rathi.. i agree with u that this is not a explanation..
but why u have put "ji" with ur name ??

huh ?

bhut izzat kamani hai kya ?
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you mak a explicitly call to the same class constructor with this() or to the super class with super() inside constructor you are not supposed to use instance method, instance fields, "this" keyword and "super" keyword.



if you look at the above code f1 is correct because it is staic.
you ca refer to static fields and static methods inside this() and super()
 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vishnu prakash:

you ca refer to static fields and static methods inside this() and super()


Thank you Vishnu ,

But , How can we have methods in this & super keyword .

Please reply ...

thanks .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic