Reputation: 103
I read a lot about difference between class,object and reference. But I have question in mind.So please help me in solving that. Suppose
Class A //declaration of class
{
//
}
main()
{
A s1 = new A();
A s2 = new A();
}
Now Question is that In above code that how many there is different object and instance in the memory.?
If one says there is two different object and two instance (i.e s1 and s2) then if we change in one instance suppose s1 then it also reflects changes in s2 also. Which shows that there is only one object present in the memory.
So How can we made different objects in the memory of same class A?
Thanks in advance!!
Upvotes: 1
Views: 153
Reputation: 29186
In your example code, there are 2 instances of class A
in memory. s1
and s2
each point to a memory address where a A
instance lives.
Using the words object
and instance
tend to mean the same thing, so you could use them interchangeably.
Now, if you did this:
main()
{
A s1 = new A();
A s2 = s1;
}
In that case, both s1
and s2
point to the same object/instance (same memory address). So if you made changes to either s1
or s2
then they would be reflected either way.
EDIT:
A little extra explanation...
main()
{
A s1 = new A();
}
In the above code, the variable s1
is instantiated to a new A
instance. Two things have happened here
A
has been created in the heap memory region.s1
stores (on the stack) the memory address in the heap.s1
will be created on the stack, and it is effectively a pointer to the memory address in the heap where the A
instance has been created.
Hopefully this extra bit of info is helping to clarify things.
EDIT:
You can create as many instances as the memory on your PC will allow.
main()
{
A s1 = new A();
A s2 = new A();
A s3 = new A();
// etc until you run out of memory.
}
I strongly suggest you look at buying some books to help you grasp the fundamentals of .NET programming. My favorite book, and one I turn to a lot, it CLR via C# 4th Edition
. You can't go wrong with that book.
Upvotes: 1
Reputation: 14716
As opposed to your statement, "if we change in one instance suppose s1 then it also reflects changes in s2 also", the changes in s1 will not reflect in s2. Hence there are two instances in memory.
You could possibly be confused because a class can contain a property or field which is a reference to yet another object. Then you can perfectly reference the same object in both s1 and s2:
s1.s3 = new B();
s2.s3 = new B();
In that case of course, changing anything in s1.s3
will reflect in s2.s3
.
As for the title of your question, an Object is an Instance of a Class.
Upvotes: 1
Reputation: 27256
According to the MSDN website:
An object is basically a block of memory that has been allocated and configured according to the blueprint. A program may create many objects of the same class. Objects are also called instances, and they can be stored in either a named variable or in an array or collection
Objects and instances are the same thing, and they belong to a class (are created from classes) by your code, you have created two objects in memory, and the variables s1
and s2
are references to these objects.
Upvotes: 1