• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can not call explicit Constructor

 
bhadule bhadule
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchies,
can anybody plz explain me why this bellow code is not getting compile?


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();
}
}


Regards
Hrushi
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope this helps

Answer
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Check this out...



Explicit Constructor invocation


[ October 26, 2005: Message edited by: A Kumar ]
 
bhadule bhadule
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry vidya sagar,
my default constructor is calling to the one argument constructor by the this keyword....what's wrong with this?....

Regards
hrushi
 
bhadule bhadule
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi A Kumar,
i read the article and i got the statement...


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.

but why this is not allowed?
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because this has an argument of instance variables

if u replace with some constant it will work
this(1.2f);
 
Sowjanya Chowdary
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bhadule bhadule:

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.

but why this is not allowed?


What is the reason that the instance variables or methods are not allowed in explicit constructor invocation?
Anyhow instance variables will get thier default values.
Please anybody clear.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is the reason that the instance variables or methods are not allowed in explicit constructor invocation?
Anyhow instance variables will get thier default values.
Please anybody clear.


instance variables get their default values only after its superclass constructor is Executed.
[ October 26, 2005: Message edited by: vidya sagar ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...

And the superclass here happens to be the Object class..

Right?
 
Balaguru Krish
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bhadule bhadule:
Hello Ranchies,
can anybody plz explain me why this bellow code is not getting compile?


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();
}
}


Regards
Hrushi
 
Balaguru Krish
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry For Previous Reply, I did not add my reply to it.

Problem lies with statement -> this(f);

The problem is we cannot refer to an instance while invoking explicit
constructor.

Hence it must be replaced with this(<constant-value> ;


Originally posted by bhadule bhadule:
Hello Ranchies,
can anybody plz explain me why this bellow code is not getting compile?


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();
}
}


Regards
Hrushi
 
Sowjanya Chowdary
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vidya sagar:


instance variables get their default values only after its superclass constructor is Executed.

[ October 26, 2005: Message edited by: vidya sagar ]


Oh yes, Thanks Vidya Sagar and Kumar.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Another thing

about super() and this() ......in inheritance

super() will be put implicitly...before explicit this()..

but explicitly super() and this() cannot go together in a constructor

since both of them have to be the first statement defined in a constructor..

So considering this....when this(f)...has been reached..it should have

already completed the execution of the superclass constructor...

which happens to be Object..here..and f should be having the default value..

according to the above post...

Then why the error? i dont know..

Ofcourse if you go by the JLS...it should throw an error if we pass instance

variables..But still the why remains..

Hope someone provides an answer...

[ October 27, 2005: Message edited by: A Kumar ]
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Then why the error? i dont know..


Hi Kumar

When we add this in the constructor(Explicilty),JVM doesnot add
super implicitly in that constructor.

Rather it add super in the other overloaded constructor(in the same class), which we called through this.

Note : Super or this should be first statement in constructor,but both cannot be in one constructor.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vidya...

I got the point you are trying to convey....You are right....

I should have been careful about that...

Thank you

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic