- Feb 8, 2004
- 12,604
- 15
- 81
Just learning terminology right now, seems like they are. All objects of class X have Y attributes, all instances of class X have y attributes. Interchangeable no?
I could say "I have 10 objects of type Person" or I could say say "I have 10 Person instances".
To me, "object" is the general term used for any object whereas "instance" refers to a single type of objects.
Generally speaking, yes, they can be used interchangeably. But not always.
The problem is that the term object is used in a couple of different contexts with very similar but not identical meanings, more then one of which apply in Java. In the general OOP sense, an object is an instance of a class.
In Java specifically, object is also a class itself, from which all other classes inherit. Most of the time the distinction between the OOP concept and the Java mother class is pointless, as they overlap to a vast degree.
But it's not 100%. All instances of the string class are objects in both senses of the word. But if I were to say "Compare two strings as objects" then 'objects' refers specifically to the mother class - an important distinction because of how operator overloading.
99.99% of the time you can use either term and have them function equivalently. Just be aware there are those few times when they're not.
In my case, if I see "String str;", I typically consider that an "object", but in regard to the definitions provided beforehand, that wouldn't work, because it's an uninstantiated or null object (there I go again).
Just learning terminology right now, seems like they are. All objects of class X have Y attributes, all instances of class X have y attributes. Interchangeable no?
I've always seen the instance as a sub part of the object. When you think of the object, it is comprised of both the static and dynamic functions and variables, while the instance is defined and differentiated by its unique combination of the dynamic component.
I've always seen the instance as a sub part of the object. When you think of the object, it is comprised of both the static and dynamic functions and variables, while the instance is defined and differentiated by its unique combination of the dynamic component.
That's too complicated in my view, but perhaps it makes more sense too look at it that way for Java, I don't know. In my view an instance, or an object, is a specific runtime example of a class or type. Of course, a class or type is also an instance in many modern languages. In C# a class is an instance of the Type class.
In Java, the class itself is represented by an instance of the Class<T> class.
I'm not really a fan of stating that an Object also contains the methods. I can't think of a great reason, so I'm going to fall back on semantics! In Java, when you serialize an instance, you call "writeObject(Object)" which does not write the methods out (only non-transient and non-static member variables). Therefore, the methods are not part of the object.![]()
I could say "I have 10 objects of type Person" or I could say say "I have 10 Person instances".
To me, "object" is the general term used for any object whereas "instance" refers to a single type of objects.
