wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt on constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt on constructor" Watch "doubt on constructor" New topic
Author

doubt on constructor

R .sourav nayak
Ranch Hand

Joined: May 14, 2006
Posts: 67
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 31, 2004
Posts: 11343

  • 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.


  • "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: doubt on constructor
     
    Similar Threads
    Compilation error
    Doubt in synchronized....
    Casting Difference of Float.POSITIVE_INFINITY
    "this " & "super"
    doubt regarding basic primitives