Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

where is the object created?

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all where or when in the following code is the object created when Animal(10) is called:
public class Animal
{
public Animal()
{
super();
}
protected Animal(int legs)
{
this();
}
}
-Thanks
Sriram
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sriram Chintapalli:
hi all where or when in the following code is the object created when Animal(10) is called:
public class Animal
{
public Animal()
{
super();
}
protected Animal(int legs)
{
this();
}
}
-Thanks
Sriram


A new object is created by the time all the constructors calls of its class are resolved, i.e. right after the call to this(); returns.
 
Sriram Chintapalli
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for answering Vad. I made a minor change to the program:
public class Animal
{
public Animal()
{
super();
}
protected Animal(int legs)
{
this();
//here
System.out.println("print this");
//or here
}
public static void main(String[] args)
{
Animal a=new Animal(10);
}
}
So the object should be constructed after the print statement(//or here) right???
-sriram
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the way you can confirm this is to throw an exception within the constructor, like this:

Obviously, the object is not created until the constructor is finished executing or m would not be null. Just another way to look at the same thing.
 
Sriram Chintapalli
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot Vad!that was a some neat code Corey, thanks!
-sriram
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey,
Is it necessary to write throw statement inside. I observe some coders just write throws clause (wtihout throw in the method).
is this allowed? if yes, why?
thanks.
Shivan
 
A berm makes a great wind break. And we all like to break wind once in a while. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic