In Java or other object-oriented programming languages, 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 things or different? Why we sometimes use object and sometimes instances if they are the 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 like your car, my car, a red car or a blue car. They are also known as instances of the car.
One reason for 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 at next section.
One reason for 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 at 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.
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.
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.
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 an explanation based upon context. In short, use Object to talk about java.lang.Object class and use the instance to talk about the object of OOP.
And Now quiz time, if you create 5 object of a class in Java with same values and compare them using == operator in Java what will be the value of them? true or false?
There is NO difference between instance and object PERIOD
ReplyDeleteThis 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.
ReplyDeleteInstance 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.
ReplyDeletethanks.your explanation helped a lot.
Deletecan you explain it more clearly
Deletethank you
ReplyDeletethanks friend!!!
ReplyDeleteThat is a GREAT explanation. Thank you
ReplyDeleteFinally someone made me clear of these confusing terminologies. Thanks Bro..!
ReplyDelete