Last post Dec 20, 2017 01:19 PM by PatriceSc
Dec 19, 2017 01:16 PM|tridip1974|LINK
i knew that object and instance both are same more or less. but when i search the same by google i found a different version which is as follows
Note the difference between the relationships: Object is a copy of the class.
Instance is a variable that holds the memory address of the object. You can also have multiple
objects of the same class and then multiple instances of each of those
they try to say object is something which reside in memory and instance is something which refer to that memory address....mean just pointer.
is it true?
anyone like to explain the same in more detail way?
Dec 19, 2017 01:50 PM|PatriceSc|LINK
It goes a bit far IMO and talking about an object as being a "copy" of a class looks weird if not just plain wrong. I would say an object IS a class instance. I'm talking perhaps about instances when I want to distinguish multiple object variables pointing
to the same object instance.
IMO "object" is closer to the real world "object" concept and is basically a kind of shortcut for programmers (ie I believe everywhere a programmer is using just "object", it could be replaced with "object variable" or "object instance" to tell more accurately
what you want to mean).
But it would be nitpicking...
Dec 19, 2017 02:13 PM|tridip1974|LINK
where object store in stack or heap? i guess object store in stack and object related data store in heap. instance variable actually point to that memory address which store in stack...i am right?
Dec 20, 2017 07:32 AM|Cathy Zou|LINK
In object-oriented terminology, a Class is a template for Objects and every Object must belong to a Class.
There is no memory space allocation for a Class when it is crated, while memory space is allocated for an Object when it is created.
Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM .
Understand the concept of value types and reference types.
Value types are types which hold both data and memory on the same location.
A reference type has a pointer which points to the memory location.
Object is A reference type
e.g: class1 cls1 = new class1();
we have created an object. When this line is executed it creates a pointer on the stack and the actual object is stored in a different type of memory location called ‘Heap’. ‘Heap’ does not track running memory, it’s just a pile of objects which can be reached
at any moment of time. Heap is used for dynamic memory allocation.
One more important point to note here is reference pointers are allocated on stack. The statement, Class1 cls1; does not allocate memory for an instance of Class1, it only allocates a stack variable cls1 (and sets it to null). The time it hits the new keyword,
it allocates on "heap".
Dec 20, 2017 01:19 PM|PatriceSc|LINK
It should be considered as an irrelevant implementation detail.
You may saw sometime this being presented as what makes the difference between a value type and a reference type when it is rather a consequence ie a stack is convenient for throwable small values while the heap make sense for allocating bigger longer term
blocks. Plus as you said you have the object data itself plus the reference to this object (which is a pointer and likely goes on the stack).
You have articles or you could use the debugger to deal in details with memory storage but I see little interest. What matters though is the reference vs value type stuff as well as passing parameters by value or be reference from a semantic point of view.