Difference between instance and Object in Java

In Java or other object oriented programming language, we often use Object and instance word interchangeably, but sometimes it confuses beginners like hell. I have been often asked several times, whether object and instance are the same thing or different? Why we sometimes use object and sometimes instance if they are same thing etc? This gives me the idea to write a little bit about it. I will mostly talk about Java conventions perspective. Just like we use word function in C or C++  for a block of code, which can be called by its same, but in Java, we refer them as methods. In Java functions are known as methods, similarly, objects are known as instances in Java. You have a class, which represent a blueprint of a real world thing e.g. Car, and object represents a real world car e.g. your car, my car, a red car or a blue car. They are also known as instances of the car.

One reason of calling instance may be because they are a representation of that class at a particular instant. In practice, use the instance to say about one particular object, and use an object to talk about many objects. By the way, it's also worth remembering that Java has a class named Object, or java.lang.Object, which is the master class and every other class extend it.

This is another reason why using instance is better because it will minimize confusion. So use Object when you want to talk about java.lang.Object and use instance when you want to talk about the object of OOPS. Let's take a look more closely in next section.




Object vs Instances

The basic concept of Object Oriented Programming (OOP) revolves around two things, Class, and Object. The class is the blueprint. The Object is an actual thing that is made up using that 'blueprint' (like the car example given above).  You cannot see the instances, all you see is code, which is your class. Object or instance are created at run-time and they are created in a specific memory area called heap memory.

Each instance consumes some memory depending upon how much and what value you store. For example "Java" is an instance of String class and holds memory required to represent those characters and to store some metadata. You might have also heard about class method vs instance methods, right? look we hardly call object method.

There is no harm calling instance as an object but if you are following rest of Java convention then why not this one. Do you call Java method function? No right, then there is no point calling instance as an object, it will just create confusion nothing more.

If you are senior Java developer or a trainer then it's your responsibility to pass right terminology to junior developers. Whatever they will hear from you, it will go a long way, so make sure you feed clear and concise information.

Difference between instance vs object in Java

That's all about the difference between Object and Instance in Java. In general, it’s better to treat instance and object as the same thing to avoid confusion, but if you follow Java convention, better call them instance. By the way no matter, what you do;  people will use it as per their convenience and you can't argue with everyone that, no you are talking about the instance, please use instance word, or, No, you are talking about an actual object, please use object word etc. I would derive explanation based upon context. In short, use Object to talk about java.lang.Object class and use instance to talk about the object of OOP.

8 comments:

  1. There is NO difference between instance and object PERIOD

    ReplyDelete
  2. This just shows how important it is to use right terminology. It avoids confusion. I have seen programmer referering array as static array and ArrayList as dynamic array. Well, static array in Java could have something very differnet meaning because Java has static modifier. Instead of that just call array as fixed size.

    ReplyDelete
  3. Instance is memory allocation on the ram at particular moment. So if you think about instance you should immediately ask the question that instance of what? if instance is for class then it object. So in memory first instance is created and then object is created.

    ReplyDelete
    Replies
    1. thanks.your explanation helped a lot.

      Delete
    2. can you explain it more clearly

      Delete
  4. thanks friend!!!

    ReplyDelete