• 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

supertype constructor ??

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}

This gives a compile error, stating that
<<cannot reference f before super type constructor has been called this(f); >>

Doesn't instance variables get default initialization and can't it not be accessed by all the methods in that class ?? Why can't we access f in the constructor ??

Regards,
JP
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My compiler gives this error. this may be a lot more clear:

Cannot refer to an instance field f while explicitly invoking a constructor
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi JP Ravi,

You're not being able to use variable 'f' because in inheritance, first
base class is initialized and then subclass. I think we can't access derived class variables as they have not yet comes into existence. May be this is the reason that this(f) is giving compilation error.

Pls correct me if I'm wrong.


Regards,
Ritu
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Member variables gets initialized only after the call to the constructor completes.
To be more specific the constructor is intended to construct or initilize the members.
[ October 17, 2005: Message edited by: Srinivasa Raghavan ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can not referance f before the pre Constroctr called.the float f has not existed
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why it can print 0.0 as following:

public class Test19 {
float f;

Test19(){
//this(f);
//f = 3;
System.out.println(f);
}

Test19(float f){
System.out.println(f);
}

public static void main(String args[]) {
Test19 t = new Test19();
}
}
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the Java Language Specification Section 8.8.7.1 it states:

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.



Referring to the original question: The constructor of class Object has not been called before f is referenced in the explicit call to this(f). It is not done until the constructer Test19(float) is executed. So according to the above specification an error must be signalled by the compiler.
reply
    Bookmark Topic Watch Topic
  • New Topic