wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor doubt" Watch "Constructor doubt" New topic
Author

Constructor doubt

Neha Monga
Ranch Hand

Joined: Mar 13, 2007
Posts: 34
Hello All,

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


Compiler error at line 1 - Cannot reference 'f' before supertype constructor has been called.

Why does the compiler complains for the above code? Per my understanding, 'f' is initialised to 0.0 before any statement in any of the constructors is executed.

Pls explain.

Thanks in advance!
Neha
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

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


Intance variables get their default values ,after completion of executing the super class constructor.At line 1 you are calling the same class constuctor with f .Before to this point there is no call to super class in this case Object class.So it does'nt have default value.


Thanks
Anil Kumar
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Notice that the error message refers to the supertype constructor being called.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Neha,

There is call to super() or this() from every constructor. If you don't write
any of both explicitly there is implicit call to super() that means calling
super class default (no-arg) constructor. And until the super class
constructor completes you can't use the subclass member variables because
they can't be used in the explicit constructor call as JLS specification says. It is only possible when super class has completed.


[EDIT] Before super class has completed, sub class members get thier
default values, default means 0 for int 0.0 for float/double and null for
references.



Thanks,
[ May 29, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
Hi Neha.
I have been thinking about exactly the same question and I think the answer will be an unsatisfying "because the developers of java decided against it". You're right, the instance variables have been given default value at the time the super constructor is called, so it COULD be allowed to use them as arguments.
Otherwise, it is not necessary to allow it, as you could as well take 0 or false or null as arguments with the same effect...
Neha Monga
Ranch Hand

Joined: Mar 13, 2007
Posts: 34
Okay..I got it right...

Thanks all!
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
anil wrote:
Intance variables get their default values ,after completion of executing the super class constructor


chandra wrote:
until the super class constructor completes you can't use the subclass member variables because they are not initialized.


That's not true, look at this code:


which prints the value of the name field in Class2 BEFORE the super constructor has completed. The output is "null"
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
Notice that the error message refers to the supertype constructor being called.


Barry, could you please explain, what you mean? I do not understand. When trying to compile the given code of Neha, i get the error message:
"Cannot refer to an instance field f while explicitly invoking a constructor". Super Constructor is not mentioned there.
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

Sasha

--------------------------------------------------------------------------
public class Main {
public static void main(String[] args){
new Class2();
}
}
class Class1 {

Class1()
{
super() //line 3 Object constructor called [by default compiler will provied]
method();

}

void method(){}
}
class Class2 extends Class1{

Class2() //line 1
{
super();// line2 Class1 constructor is called [Compiler will provide]
}
String name = "Class1";
void method()
{
System.out.println(name);
}
}
-------------------------------------------------------------------------


After executing of line 3 you are calling method().By that time the super class constructor runs (In this case Object).

See the modifications of your code .I have added the default things provided by compiler.


Thanks

Anil Kumar
[ May 29, 2007: Message edited by: anil kumar ]
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115

Yes, the super.super-constructer (Object) ran. But before the super-constructor (Class1) completed, the instance-variables of Class2 have been given default values. That happened before the (compiler added) call to super() in Class2-constructor was executed. From K&B:
Remember, when an object
is constructed using new (as opposed to being deserialized), the following things happen (in this order):
1. All instance variables are assigned default values.
2. The constructor is invoked, which immediately invokes the superclass
constructor (or another overloaded constructor, until one of the overloaded
constructors invokes the superclass constructor).

So I still think that the question of Neha is justified and even more: using instance variable as arguments in a constructor-call to super could as well be allowed. (As I said before: no problem that it is not...)
[ May 30, 2007: Message edited by: Sasha Ruehmkorf ]
Neha Monga
Ranch Hand

Joined: Mar 13, 2007
Posts: 34
Hello Anil!

With the help of above example do you intend to say that the instance variables in a class will be initialised to their default values only when the constructor of the top most class i.e. Object has completed. Even if the constructors of intermediate classes haven't completed, the variables in the subclass(which is being instantiated) will be initialised to default values.

Pls confirm!

Thanks Neha
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707




Thanks,
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
super(xx,yy);//1) this gives compiler error

Yeah, that is exactly the point of the whole discussion. Why is this given compiler error?
Neha Monga
Ranch Hand

Joined: Mar 13, 2007
Posts: 34
Hey Sasha!

This all is very confusing....
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
JLS Section 8.8.7.1:

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 top most question (Posted by Neha):
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 Test(float) is executed. So according to the above specification an error is generated by the compiler.




Thanks,
[ May 29, 2007: Message edited by: Chandra Bhatt ]
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

Neha

Yes....

Once the super class constructor has completed execution,then all the instances variables of the subclasses will get their default values.


Thanks

Anil kumar
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
Hey Sasha!

This all is very confusing....

Don't capitulate

Chandra, I have read the Language Specification, but it just says, that it will give a compiler error. The question was not "Will this give a compiler error?", Neha asked "Whydoes the compiler complains for the above code?" Obviously the compiler complains, obviously it is forbidden, but why?

Once the super class constructor has completed execution,then all the instances variables of the subclasses will get their default values.

Anil, haven't we just prooved, that the default values of the subclass are given BEFORE the superclass-constructor is even invoked?
Neha, I am also getting very confused now
[ May 29, 2007: Message edited by: Sasha Ruehmkorf ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

Once the super class constructor has completed execution,then all the instances variables of the subclasses will get their default values.


Default values means 0 for int 0.0 for float/double and null for reference
variables for example. And subclass member variables get this before super
class has completed and once super class constructor completes subclass
members get what values are assigned when they are declared, or in the
constructor.



Thanks,
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Sasha:

"Whydoes the compiler complains for the above code?" Obviously the compiler complains, obviously it is forbidden, but why?


There are limitations: when a line int a=10; is encountered in the sub class,
memory has to be allocated so what value to place in the memory location
for "a", we can't place 10 because till this time super constructor has
not been completed so 0 will be ok. But we have limitation what JLS says
in this regard that we can't use the subclass member variable in the
explicit constructor call because the subclass member variables are not
completely in existence and wont be until they are given the value
10 in my example or the subclass constructor code does some initialization
for it.


Thanks,
[ May 30, 2007: Message edited by: Chandra Bhatt ]
rajesh baba
Greenhorn

Joined: May 19, 2007
Posts: 25
hi friends,
*calls to this() and super() cannot be in the same constructor.you can have one or other but never both.
in your program you provided a this(f) as per above rule complier will not provide super() but with out calling super you cannot initiate f so with out initiation you cannot use it in this(f) .
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Sasha Ruehmkorf:


i get the error message:
"Cannot refer to an instance field f while explicitly invoking a constructor". Super Constructor is not mentioned there.


You are using Eclipse - that is not using the Sun compiler which does produce the message referring to the supertype constructor.
SelvarajC Chennappan
Greenhorn

Joined: Aug 20, 2009
Posts: 6
you will get better idea

http://www.javaworld.com/javaworld/jw-03-1998/jw-03-initialization.html?page=5

Regards,
Selvaraj
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Hi SelvarajC,

Welcome to java ranch!! Don't wake up an old thread which is already almost 2 1/2 years. The person who created this thread is not around here to respond.

Thanks,

Naveen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor doubt