• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compiler error

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is illegal forward reference.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not clear ...
Can any body explain it in simple words ...

thanks .
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok: this(f); is illegal.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Jeroen ,
for your information ... this was not explanation ... understand .
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
I love a good mentalist. And so does this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic