class, object and instance

What is the difference between class and object?

Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects

Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don’t change.

The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed.

An Object on the other hand has a limited lifespanObjects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.

——————————————————————————————————–
What is the difference between object and instance ?

When we create object from a class, it is created on stack or on heap. The existance of an object in memory (stack or heap) is called instance of an object.

Hence Instance and object is used interchangeably.

instance means just creating a reference(copy) .

object means when memory location is associated with the object( is a runtime entity of the class)  by using the new operator.

——————————————————————————————————–

Reference material

What is the difference between stack and heap ?

Stack and Heap are just two different datastructures for storing objects in memory. 

Stack is FILO – first in, last out. Basically you think of it like a big bin. You put stuff in the bottom, and keep piling stuff on top (stacking it up). You can only put stuff on top, or take stuff off the top. You can’t remove stuff from the middle.

The stack is used for instance with method calls to store the local variables. When you call a method, the memory for the local variables is allocated on the stack. When you return from the method, that memory is released, as it is no longer in use. As methods follow a strict order for control flow the stack is perfect for them.

Heap is exactly that – just a heap of objects that you can access any way you want. Objects created using “new” reserve memory from the heap, because it can’t predict when you will release that memory.

Garbage collection runs on the “Heap”, as this is the memory that gets fragmented. With the stack you know you need everything in it.

Hope that explains the basic difference without confusing you too much.
Probably google can explain it better than I can if you’re still interested.

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s