aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many number of Objects created 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 "How many number of Objects created" Watch "How many number of Objects created" New topic
Author

How many number of Objects created

Ankur Mahajan
Greenhorn

Joined: Feb 03, 2008
Posts: 8
Hi All

I have small doubt regarding, number of objects created in case when a sub class is instantiated.

e.g.


Now the question is, when constructor of class B is called it will call the constructor of Class A and then from A contructor of Object class is created.

So a total of 3 objects should be created in heap.
Please some validate the same.

If this is true then how it is done in case of abstract class as its objects cannot be created.


TIA
Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
Hi! Ankur,

This is not the way you are percieving.Actually Only 1 object will be created in this scenario and the constructor calls will be same as you have stated.

constructor call is required to initialize those members of the super classes which are derived from the super class in derived class.
As the data members which are being derived into the derived class need to be initialized by their respective constructors(constructors are not inherited).

In case of Abstract class ,its true that they can't be instantiated but if some class has extended it in that case the members of that abstract class derived into the Sub class need to be initialized by the constructor of Abstract class only.

Hope you understand my point.


Regards...
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Just Remember this, When an Object is Created, its constructor is called. But when a constructor is called, it does not mean that an Object is created.

Consider the following scenario

class A {
int x;

A()
{ this(10);
}
A(int z)

{
x=z;

}

public static void main(String args[])

{
A x = new A();

}

}

In the above case, the default constructor calls the overloaded constructor.
So in all 2 constructors are called, but Number of Object created is 1.


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by chander shivdasani:
In the above case, the default constructor calls the overloaded constructor.
So in all 2 constructors are called, but Number of Object created is 1.


LOOKS GOOD
Ankur Mahajan
Greenhorn

Joined: Feb 03, 2008
Posts: 8
Thank you all for your replies.
I have understood the concept now.
Gaurav tyagigaurav
Greenhorn

Joined: Jul 30, 2008
Posts: 15
Hi Guys!!!
I had exactly the same doubt that is stated in the original Q. Didnt know where to look for. Thanks to Java Ranch found the soln. here. Java ranch rocks [ ]. I however in the mean time was wondering how could i write/test the code to find out how many objects get created. Any idea how can we modify the above code to find out the number of objects created?

Cheers
Gaurav
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

how could i write/test the code to find out how many objects get created.


The simple way i think is to use a initialization blocks,Which runs every time when a class object is created.


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
 
Don't get me started about those stupid light bulbs.
 
subject: How many number of Objects created