wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Which came first constructor or object ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Which came first constructor or object ?" Watch "Which came first constructor or object ?" New topic
Author

Which came first constructor or object ?

Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Hi folks!
What happens first,
constructor call or object creation?


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

The objects constructor runs before you can use it. So i guess its the constructor


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
hey john!
try this...
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Full object instantiation is complete when the constructor runs. The "this" object can be used to instantiate variables like so... this.variable = value

So in my opinion, only when the contructor exits is your object ready for use, just in case you want some variables that it has to be instantiated in some way.

You wont get this as a question in the exam by the way.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Steps of constructing an object:
1) The Java virtual machine allocates enough unused memory in the heap that can be used to hold the object;
2) The Java virtual machine insures proper initialization of objects. Set fields to default initial values (0, false, null)
3) Call the constructor for the object (but don't execute the body of the constructor yet)
4) Invoke the constructor of the superclass
5) Initialize fields using initializers and initialization blocks
6) Execute the body of the constructor
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Thanks john and wise.
kwan Jang
Greenhorn

Joined: Sep 24, 2006
Posts: 16
if you new an object,after initialization,the object's parameter will endow the variable,the last is constructor,it will bestrow the variable,and finish creating the object!so i think object is the first,constructor is going with it,but i think constructor is finish first!that's just my own opinion.so i don't know if i say that is right!
Alangudi Balaji Navaneethan
Ranch Hand

Joined: Apr 28, 2004
Posts: 42
Hi,

You can consider the constructor as a process to create an object. and object as the result of constructore. If the constructor fails there will be no object. an object creates itself... through constructor.

The hen creates the egg of itself and come out the egg... do you understand....


if you think you can you r right<br />if you think you can not you r double right
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Thanks kwan and balaji! I would conclude,
"Constructor call and execution... is part of the object creation."

I would like to konw few more things...
1. An abstract class can not be instantiated or its object can not be created.
2. However inheriting an abstract class and instantiatng the same(child-class), calls the (parent)abstract-class's constructor.

Any comment on this behaviour?
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Hello,

Its cool.

See the JVM Specification.

1) The Constructor is the first method to be called ( after init.blocks) on the object that is being created.

2) If you think from the OO principles, an object requires initial behavior, so constructor provides that.

Refer:

http://java.sun.com/docs/books/vmspec/
Alangudi Balaji Navaneethan
Ranch Hand

Joined: Apr 28, 2004
Posts: 42
Yes,

The JVM will call the constructor of the super class in order to do some household works that the objects of the derived classes need to do.

abstract class Base
{
protected i;
protected void Base()
{
i=10;
}
}

class Derive extends Base
{
protected int j;
public void Derive()
{
super();
j = i * 20;
}

public static void main(String[] arg)
{
Derive der = new Derive();
}
}
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Something that interest me is,

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServlet.html#constructor_summary

"Constructor Summary:
Does nothing, because this is an abstract class."

When an abstract class can not be instantiated, and the only to make use of class is through inheritance, then do we really need a constructor for such a class?
Pedro Lobato Sena
Greenhorn

Joined: Apr 25, 2006
Posts: 16
Originally posted by Akhilesh Trivedi:
Something that interest me is,

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServlet.html#constructor_summary

"Constructor Summary:
Does nothing, because this is an abstract class."

When an abstract class can not be instantiated, and the only to make use of class is through inheritance, then do we really need a constructor for such a class?


Sure, supose you have an abstract class that contains some objects, in the constructor you could change the states of those objects, specially if you have a constructor that requires some arguments.


SCWCD<br />SCJP<br /> <br />Pedro Henrique Lobato Sena
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which came first constructor or object ?
 
Similar Threads
Constructors in Inheritance
Constructors are not inherited.
Static and Instance Blocks..
superclass constructors
Test your knowledge