File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object Vs instance

 
eswar kumar
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between an instance and a Object ?
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that capitol O in Object means (hopefully) that you are referring to the class of objects defined by the class java.lang.Object.
If you are asking what the difference between an "object" and an "instance", then simply an object is any tangible item in the JVM's memory. "objects" will be instantiations of classes. Instances of course means the same thing.
There are probably some semantic differences from what I've said here, based on OO terminology -- which I'm sure somebody will toss in.
But all in all, all objects are instances of classes, even class objects (ex String.getClass()) -- "Object" proper -> java.lang.Object is a class of objects, which may be instantiated at various times during your program to achieve some wonderful thing.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd call little-o object and instance the same thing ... an object is an instance of a class. Some times it is important to be very precise about class and object ... class being a definition of a thing and object being an example thing of that class.
For example, when you write a method call in your code the compiler checks that the class of an argument you pass matches the class of the parameter on the method. But at run time, you're passing a real object and the JVM checks that the object you are passing is an instance of a class that matches the method.
By the way: Capital-O Object is really confusing because it is (get this!) a class. Yikes! That's purely a Java thing - blame Sun for confusing us all.
Reading back on this, I think Nathanial and I said exactly the same thing with slightly different emphasis. I hope reading it twice helps!
[ December 13, 2003: Message edited by: Stan James ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A point that one might consider a subtle difference between the term object and instance (though I agree with Stan and Nathaniel that for all intents and purposes they mean the same thing) is that an instance is a single instantiation of a class with a unique identity. That may not seem important except for the fact that it is one of the tenets of OO theory and we use that fact in all sorts of ways in our programming, for example we can use any instance as key in a Map and be assured that the value stored under that key can be retrived later (assuming that the hashcode and equals methods for the class have not been overridden in such a manner that two different instances might appear to be the same object).
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With three very good responses already, I hesitate to add another, but I love chewing on this stuff so ...
I also see subtle differences between object, class and instance.
  • An object is the logical concept of a type of thing: a car
  • A class defines the data and behavior of an object: Car { licensePlate, model, color }
  • An instance is a single, unique memory allocation of a class: Car @ 0x032AD892 [ "2EJB141", "Porsche 911", Color.RED ]

  • [ December 14, 2003: Message edited by: David Harkness ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic