• 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

constructor

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {
Test(){
this(7);
}
Test(int i){
this(1.0);
Test(i);
}
Test(float f){
System.out.println(f * 2);
}
Test(double d){
System.out.println(d * 3);
}
void Test(int i){
System.out.println(i);
}
public static void main(String args[]) {
Test t = new Test();
}
}

output is 3.0,7 ....explain this
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean please explain this?
[ June 30, 2006: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use tags.
Please use a meaningful topic title.
Why use a method Test with the same name as your class? Perhaps if you rename the method it will be easier to understand what is happening.

What type is the literal 1.0 and what constructor will be called?
[ June 30, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this(1.0);
calls Constructor which takes double d as an argument

Test(i);
calls public void Test(int i) method.


I hope this helps.


Shriniwas
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this(1.0);
calls Constructor which takes double d as an argument

Test(i);
calls public void Test(int i) method.


I hope this helps.


Shriniwas
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First the no arg constructor is called. Which calls the constructor with the integer as a formal paramter passing 7 as a parameter. Then there is a this() call which calls the constuctor with double as formal parameter which prints 3.0 then the Test method is called from the constructor taking int. Since it recieved 7 as a parameter same gets printed.

I hope you asked for the reason why 3.0 and 7 is the output.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What will happen here? Will it compile?
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you try & post the answer.
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you try & post the answer.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shrinivas Mujumdar:
Why don't you try & post the answer.



Because it is far better to "teach a man to fish rather than giving him a fish"
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic