Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compiler error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compiler error" Watch "compiler error" New topic
Author

compiler error

Puja S
Ranch Hand

Joined: Jan 06, 2005
Posts: 51
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

Joined: Oct 12, 2000
Posts: 5093
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.


42
Anand Ko
Ranch Hand

Joined: Dec 03, 2003
Posts: 79
This is illegal forward reference.


Anand<br />SCJP 1.4, SCWCD 1.4, SCEA 5.0(1/3)
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 107
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

Joined: Oct 11, 2004
Posts: 3830
Not clear ...
Can any body explain it in simple words ...

thanks .
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
ok: this(f); is illegal.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Hey Jeroen ,
for your information ... this was not explanation ... understand .
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
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 ?


Thanks and Regards, Amit Taneja
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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()


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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 .
 
Consider Paul's rocket mass heater.
 
subject: compiler error
 
Similar Threads
Constructor
compiler error?
Constructor
supertype constructor ??
constructors