Difference Between Object and Instance

Object Oriented Programming (OOP) is one of the most popular programming paradigms. In OOP, the focus is on thinking about the problem to be solved in terms of real-world elements and representing the problem in terms of objects and their behavior. Many programming languages that support key aspects of OOP (called OOP languages) have the class as the main programming tool. They are called class-based. Classes are an abstract representation of real world objects. Classes have properties called attributes. Attributes are implemented as global and instance variables. Methods in the classes represent or define the behavior of these classes. Methods and attributes of classes are called the members of the class. In very simple terms, a class is a blueprint or a template for a specific real life object. So, an object is the memory block(s) used to store necessary information according this blueprint. Instance is a memory block that refers an object.

What is an Object?

Objects are the results of instantiating a class. Instantiation is the process of taking the blueprint and defining each attribute and behavior so that the resultant object actually represents a real life object. Object is a dedicated and continuous block of memory allocated to store information such as variables, methods or functions, etc. Object is created with the use of new operator, in Java programming language. For example, if there is a class called Car, then the following can be used to create an object of the Car class.

new Car();

Here, a Car object is created by the new operator and a reference to object is returned. The new operator along with the constructor of the Car class is used to create the new object. The life span of the object starts from the call to its constructor to the time it is destroyed. Once an object is not referred, it will be removed/ destroyed by the garbage collector.

What is an Instance?

Instance is a memory block, which contains the reference to an object. In other words, Instance will keep the address of the starting memory block where the object is stored. Actually, the name of the instance can be used to access the start of the object memory area. Offsets from the starting memory is calculated by the runtime engine so that we can go to where the individual data or method references are stored. Following Java code snipped can be used to create an instance of a Car object.

Car myCar = new Car();

As mentioned above, the new operator creates the Car object and returns the reference to it. This reference is stored in the Car type variable myCar. So, myCar is the instance of the Car object created.

What is the difference between an Object and an Instance?

Object is a contiguous block of memory that stores the actual information that distinguishes this object from other objects, while an instance is a reference to an object. It is a block of memory, which points to the staring address of where the object is stored. Two instances may refer to the same object. Life spans of an object and an instance are not related. Therefore an instance could be null. Once all instances pointing to an object is removed, the object will be destroyed.