Are instances and objects the same thing in java?

Maximilian

Lifer
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?
 

Stuxnet

Diamond Member
Jun 16, 2005
8,392
1
0
No. All instances are objects, but not all objects are instances. You instantiate an object; that is, you create a "breathing" entity. A Person could be a class that defines various properties (height, weight, eye color, age, etc) and methods (walking, sleeping, eating, etc), but those definitions ("class members") don't take on meaning until you actually use it by creating an actual person.

This is a bit of an over simplification and doesn't begin to address exceptions (static members, for instance), but hopefully you get the idea.
 

iCyborg

Golden Member
Aug 8, 2008
1,388
94
91
Stuxnet, can't say I agree with your explanation which is somewhat confusing too. I thought an object is an instance of a class, which objects (in OOP sense) aren't instances?

Anyway, to me an object is an instance of a class. Instance here is a word to describe the relationship between objects/classes and is a regular English word used in other contexts aside from programming.
http://dictionary.reference.com/browse/instance

That's my take, anyway...
 

purbeast0

No Lifer
Sep 13, 2001
53,764
6,645
126
this really seems to be more a question of semantics imo.

you can have an object Person, and then have 2 different objects that extend Person, say Man and Woman objects.

then in your code you can declare a Person and assign a new Man() or new Woman() to the object.

however, you can't declare a Man object and assign it to a new Person().

personally, i dont really even use the word "instance" to describe any classes/objects in the code, unless i have a parent object that is set to be a new child class, and you just want to know what object that actual parent class is an instance of (if it's not an instance of it's own parent class object).

hope that makes any sense lol.

you may also want to check out the keyword "instanceof" in java. it is used for the exact reason i was explaining above, in a boolean operation.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
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.
 

TecHNooB

Diamond Member
Sep 10, 2005
7,458
1
76
some classes are designed to allow creation of multiple instances of an object. some classes are designed to allow only 1 instance of the object (a singleton object), and each subsequent call to the constructor only gives you a reference to the singleton object (does not create a new instance). this isn't particular to java.
 
Last edited:

beginner99

Diamond Member
Jun 2, 2009
5,320
1,768
136
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.

Yeah I see it this way too.
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
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.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
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.

I guess what you're trying to say is that "object" and "Object" are two different words with different meanings.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
I agree that in the case of Java, there's a semantic difference to be aware of... I guess it'd be best put because of how Java (mostly) obfuscates pointers from the programmer.

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 :p).
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
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 :p).

I've heard people call those "null instances", but I don't like the term.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
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 think in the specific context of your question the two terms are interchangeable. There are possible semantic distinctions between "object" and "instance" but it doesn't look to me like they're relevant to your question.
 

Schmide

Diamond Member
Mar 7, 2002
5,798
1,126
126
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.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
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.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
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.

The functions are part of the class, but are not part of the instantiated objects. The objects are purely data.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
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. ;)
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
From: http://en.wikipedia.org/wiki/Instance_&#37;28computer_science)

In object-oriented programming an instance is an occurrence or a copy of an object, whether currently executing or not. Instances of a class share the same set of attributes, yet will typically differ in what those attributes contain. For example, a class "Employee" would describe the attributes common to all instances of the Employee class. For the purposes of the task being solved Employee objects may be generally alike, but vary in such attributes as "name" and "salary". The description of the class would itemize such attributes and define the operations or actions relevant for the class, such as "increase salary" or "change telephone number." One could then talk about one instance of the Employee object with name = "Jane Doe" and another instance of the Employee object with name = "John Doe".
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
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. ;)

The methods are defined by the class and act on the instance... but I don't find the semantic distinction that useful.
 

Ulchieman

Member
Apr 7, 2003
37
0
0
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.

This.
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
Person = object
Maximilian = instance of person
KIAman = instance of person
Maximillian = Person = object
KIAman = Person = object
KIAman != Maximillian (because properties are different)