Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which came first constructor or object ?

 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks!
What happens first,
constructor call or object creation?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The objects constructor runs before you can use it. So i guess its the constructor
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey john!
try this...
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks john and wise.
 
kwan Jang
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Firefox Browser Hibernate Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic