Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on constructor

 
R .sourav nayak
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could some tell me on which order the constructors are being called from the following program:
public class Test18 {
Test18(){
this(7);
}
Test18(int i){
this(1.0);
Test18(i);
}
Test18(float f){
System.out.println(f * 2);
}
Test18(double d){
System.out.println(d * 3);
}
void Test18(int i){
System.out.println(i);
}
public static void main(String args[]) {
Test18 t = new Test18();
}
}


thanks in advance

reena
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by R .sourav nayak:
could some tell me on which order the constructors are being called from the following program:
public class Test18 {
Test18(){
this(7);
}
Test18(int i){
this(1.0);
Test18(i);
}
Test18(float f){
System.out.println(f * 2);
}
Test18(double d){
System.out.println(d * 3);
}
void Test18(int i){
System.out.println(i);
}
public static void main(String args[]) {
Test18 t = new Test18();
}
}


thanks in advance

reena


this(7) calls Test18(int i)
this(1.0) calls Test18(double d)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Test18() is called in main.
  • Test18() calls Test18(int) because literal integer values (7) are interpreted as ints.
  • Test18(int) calls Test18(double) because literal floating-point values (1.0) are interpreted as doubles.
  • Test18(double) calls Object() because there is no explicit call to this or super.
  • When Object() returns, the body of Test18(double) executes and prints out 3.0.
  • When Test18(double) returns, the body of Test18(int) executes and calls the method Test18(int).
  • Method Test18(int) prints out 7.
  • When the method returns, the body of Test18() executes.
  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic