my dog learned polymorphism*
The moose likes Beginning Java and the fly likes How many objects will be created in Heap when we have multiple instances of a specific object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How many objects will be created in Heap when we have multiple instances of a specific object?" Watch "How many objects will be created in Heap when we have multiple instances of a specific object?" New topic
Author

How many objects will be created in Heap when we have multiple instances of a specific object?

Srinivasa Kumar Meda
Greenhorn

Joined: Dec 10, 2010
Posts: 6
We know that the Objects in Java are stored in Heap. If we have two instances of a specific class, how many objects will be available in heap?

For ex,

Sample s1 = new Sample();
Sample s2 = new Sample();

s1,s2 are two instances of Sample.

1). Can you please tell me how many Objects of Sample is created in Heap to store s1 and s2?


The second question is:

If class B extends class A and

A a = new B(); is declared.

2). What is the object stored for reference a in Heap??
3). and since B extends A, how the object of B will be in Heap??


Can someone please answer the 3 questions??

Thanks,
Srinivasa Kumar Meda
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

Srinivasa Kumar Meda wrote:We know that the Objects in Java are stored in Heap. If we have two instances of a specific class, how many objects will be available in heap?

For ex,

Sample s1 = new Sample();
Sample s2 = new Sample();

s1,s2 are two instances of Sample.

1). Can you please tell me how many Objects of Sample is created in Heap to store s1 and s2?


s1 and s2 are NOT the instances, but they are references which refer to those 2 instances created using new Sample(). By references i mean- You can use these- s1,s2- to access the fields and methods of the instances. And with Every new- we get one object on the heap.

Srinivasa Kumar Meda wrote:
The second question is:

If class B extends class A and

A a = new B(); is declared.

2). What is the object stored for reference a in Heap??
3). and since B extends A, how the object of B will be in Heap??

Here- Reference type for "a" is "A", but the Object created is of type- B. Inheritance has nothing to do with- which class's Instance is created. So if you do-


You can always assign the instances of the class and its subclass to a reference of the class type. (Like the way its done in the code provided by you).


Mohamed Sanaulla | My Blog
Thakur Sachin Singh
Ranch Hand

Joined: Jun 15, 2010
Posts: 232

every call of new Sample() create a new object....so every time when you write the new Sample(), object is different from previous one.


SCJP 6- 91%, IBM DB2, IBM RAD Certified
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

Welcome to Javaranch, Srinivasa Kumar Meda


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to the Ranch!

When you say new B(); one object is created with the word "new." The type of that object is B (the constructor following "new").

In the process of creating that object, constructors are called from the top down. So the resulting instance IS-AN Object, and IS-AN A, and IS-A B.

Therefore, a reference to that object can be assigned to a variable of type Object, or type A, or type B.

So when you say A a = new B(); you have a reference of type A which is pointing to an object of type B. And that's perfectly fine, because the object B is also an A.

So here's a quiz. If you do this...

...then how many objects are there? And what are the types of those objects?

How many references are there? And what are the types of those references?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Thakur Sachin Singh
Ranch Hand

Joined: Jun 15, 2010
Posts: 232

there are four types of reference variable.

three types of A and one types of B.

and objects are two....am i right marc???
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

Thakur Sachin Singh wrote:there are four types of reference variable.
three types of A and one types of B.

Correct. To be more clear, four reference variables, three of them are of type "A" and one is of type "B".
and objects are two....am i right marc???

Correct.
Thakur Sachin Singh
Ranch Hand

Joined: Jun 15, 2010
Posts: 232

here we store one object in three reference variable a1,a3 and b1 and other object store only in a2.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

Thakur Sachin Singh wrote:here we store one object in three reference variable a1,a3 and b1 and other object store only in a2.


Just putting it in a right way- Three reference variables refer to the same one object and a2 refers to a different object. Its sometimes confusing if we say references store the object (Actually they store the objects address).
Srinivasa Kumar Meda
Greenhorn

Joined: Dec 10, 2010
Posts: 6
Thank you all for your responses. Its really informative and helpful in getting the answer for my question.

I have one associative question for this question.

The question is

Class B extends A{
}

here B is a sub class of A. i.e., B IS-a A.

How the object B is stored in Heap?? How B IS-a A can be represented in Heap? Is the Object B having A inside it while creating object B in Heap??
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

Srinivasa Kumar Meda wrote:...How the object B is stored in Heap?? How B IS-a A can be represented in Heap? Is the Object B having A inside it while creating object B in Heap??

Only one object created but everything inherited from A also contained there. That's why there should be a call to super (directly/indirectly) within the constructor.
Srinivasa Kumar Meda
Greenhorn

Joined: Dec 10, 2010
Posts: 6
Hi Vijitha,

Thanks for the reply.

You mean all the properties and methods of object A are part of the object B when the Object B is created in Heap?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

Srinivasa Kumar Meda wrote:...You mean all the properties and methods of object A are part of the object B when the Object B is created in Heap?

All the super class(es') properties will be initialized when the constructor gets completed. You can read more here about the initialization.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37970
    
  22
Apurva Gaonkar,
Your post was moved to a new topic.
 
Don't get me started about those stupid light bulbs.
 
subject: How many objects will be created in Heap when we have multiple instances of a specific object?
 
Similar Threads
RHE question - String buffer
Mock Exam Q
implementation for static and non-static methods stored in memory
Urgent String question
String puzzle