File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes where is the object created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "where is the object created?" Watch "where is the object created?" New topic
Author

where is the object created?

Sriram Chintapalli
Ranch Hand

Joined: Dec 16, 2003
Posts: 59
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
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
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

Joined: Dec 16, 2003
Posts: 59
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

Joined: Aug 25, 2003
Posts: 504
Right.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Sriram Chintapalli
Ranch Hand

Joined: Dec 16, 2003
Posts: 59
thanks a lot Vad!that was a some neat code Corey, thanks!
-sriram
Liz Brown
Ranch Hand

Joined: Oct 22, 2002
Posts: 112
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: where is the object created?