7

I have a wording / precision question. Sometimes I write "object instance" sometimes "class instance".

Isn't it that an object is always an instance of a class? Therefore "object instance" is not the correct wording, right?

Is it common to say so anyway, or just a "mistake" of mine? I have the feeling "object instance" is superfluous (doubled) because an object is always an instance and would like to know more for clarification of the terms.

hakre
  • 1,165
  • 12
  • 17

6 Answers6

15

No, it is not right that an "object" is always an instance of a class. Just for example, the standard for C (which doesn't have classes at all) defines an object as (§3.14/1): "region of data storage in the execution environment, the contents of which can represent values."

Now, it is true that using "object" to refer to an instance of a class is quite common. In some languages (e.g., Smalltalk) all objects are instances of classes. In others (e.g., C++) the term is somewhat ambiguous, because there is a C-like use of the term and a Smalltalk-like use of the term, so it's not necessarily clear whether "object" is being used to refer specifically to an instance of a class, or just to some region of data storage in the execution environment, which may be an instance of some primitive type rather than a class, or may be (for example) some dynamic storage that hasn't been initialized, so it's not really an instance of any type.

As far as "object instance" making sense, I can see one situation where it could. Going back to Smalltalk: everything in Smalltalk is an instance of a class -- even a class is an instance of a class (called the metaclass). In a case like this, I can see where it could (sort of) make sense to talk about an "object instance", when you were specifically talking about an instance of a class as opposed to the class of the class.

In fairness, however, that usage is undoubtedly quite rare (at most). The vast majority of the time, "object instance" is probably just sloppy wording.

Jerry Coffin
  • 44,385
  • 5
  • 89
  • 162
  • 1
    +1 That clarifies a lot for the terms meaning, thanks a lot. – hakre Aug 07 '11 at 16:00
  • 3
    Somewhere in Ch. 10 of *The C++ Programming Language*, Stroustrup talks about the distinction between object as region of memory and object as instance of a class, and cautions that they shouldn't be confused. It seems clear to me that the "region of data storage" definition isn't the one intended by this question. – Caleb Aug 07 '11 at 19:52
  • 1
    @Caleb: Now this is getting really great, that's mirroring my feelings. Thanks for your comment. – hakre Aug 07 '11 at 21:18
7

An object, often called an instance, is a specific instantiation of a class. If you instantiate (make an instance of) a class ten times, you get ten objects, but there's still just the one class.

Caleb
  • 38,959
  • 8
  • 94
  • 152
5

The term object can refer to (at least) three different independent concepts:

  1. An instance of a class. This is the case for object-oriented, strongly-typed, statically-typed languages such as Java and C#. In these languages, the "class" is the definition and "object" is a single manifestation ("instance") of that.

  2. An untyped area in memory containing functions and data/state. This is what you'll find in many dynamically-typed but still object-oriented languages, especially scripting languages such as JavaScript. In this case, there is no "class", and there is no "instance" either, there are just objects, period.

  3. Any addressable memory or data in a program. This was the commonly-accepted definition before OOP existed, and is still part of the C spec, as Jerry Coffin explains.

So this is essentially a two-part question, the answers being:

  • The term "object" is always correct in object-oriented languages. The term "class instance" is sometimes a synonym, but that depends on the specific language.

  • The term "object instance" is never correct. In languages that define objects, but not classes, the term "instance" is meaningless because every object is by definition an instance of something.

If you want to be safe, just avoid using the term "instance" unless you specifically need to distinguish between the type definition (class) and an instance of that type, e.g. when referring to an instance member vs. a class or static member.

Aaronaught
  • 44,005
  • 10
  • 92
  • 126
  • +1 Very nice structured differentiation for the term *object*. – hakre Aug 07 '11 at 16:32
  • I object to #1. An object is an instance of a class even in some dynamically typed languages - and I suspect in some weakly typed ones, too. I also object to #3. "Object" was a common definition for a piece of addressable storage in at least one OO language for at least a decade before Java was even invented. – sbi Aug 07 '11 at 19:04
  • @sbi: I said "this is the case for X languages", not "this is not the case for any other languages". As for #3: Which one? You'd better not say "smalltalk", because that would be wrong. – Aaronaught Aug 07 '11 at 19:14
  • #3 is part of the C++ spec too, and as such, isn't really "pre-OOP". It's also not "any addressable memory", but "any data type stored in memory". A C++ (or C, afaik) object does not exist until you actually write a value to memory. So honestly, I don't really see it being that incompatible with "the OOP meaning". – jalf Aug 07 '11 at 20:33
  • @jalf: If we're going to nit-pick, the C++ specification contains the literal text, *An object is a region of storage.* However, the C++ specification almost always delineates the type of object, e.g. "class object" or "struct object". It also frequently makes distinctions between values and objects and refers to "instances of X" (X being class, struct, the name of a specific type, etc.) So "class instance" and "object" are essentially synonyms in the C++ spec as well, except the "object" can refer to other things too. – Aaronaught Aug 07 '11 at 22:34
2

The class is the compile-time specification of the object. The object is the runtime realisation of that specification.

Therefore when you create an object you are creating an "instance of a class", or "class instance".

Gary
  • 24,420
  • 9
  • 63
  • 108
  • @Aaronaught Yeah - on reflection it was a bad call. I'll edit accordingly. Other better answers will doubtless float up. – Gary Aug 07 '11 at 16:07
2

An object instance would be something which corresponds to that object in the same way that a class instance corresponds to a class. I am not aware of any mainstream concepts that match that definition...

Also, while it's true that a class instance is always an object, objects are not always class instances : some languages support the creation of objects as literals. Consider, for instance, the JavaScript object defined as:

var bob = { name : "Bob", age : 31, birthday : function() { this.age++ } } ;

Or the OCaml equivalent:

let bob = object
  val name = "Bob"
  val age  = 31
  method birthday = {< age <- age + 1 >}
end
  • 1
    This is a good point; not every object-oriented language is also statically or even strongly-typed, so the word "object" is literally just a generic *object*. I still wouldn't use the term "object instance" though - that seems to be wrong in any language. – Aaronaught Aug 07 '11 at 16:15
0

Isn't it that an object is always an instance of a class? Therefore "object instance" is not the correct wording, right?

int i;

Disproof by counter-example.

On the other hand, "object instance" is not good, and nor is "class instance", because not every object is an instance of a class. I'd usually just say "instance" or "object".

DeadMG
  • 36,794
  • 8
  • 70
  • 139
  • In the context of your answer, how would you word/coin `int i;`? Which programming language is that? – hakre Aug 07 '11 at 15:19
  • Does it matter? No language that I know of specifies primitive types in terms of classes, but `i` is still an object. – DeadMG Aug 07 '11 at 15:21
  • 2
    ^ Except in languages where it isn't an object, like just about every language that uses primitives instead of classes for integers. Its a primitive, not an object. -1. – alternative Aug 07 '11 at 15:25
  • @DeadMG: That's what I wanted to know. You name `i` an object, I would have called it a variable. Are you referring to compile-time or runtime? – hakre Aug 07 '11 at 15:26
  • In what way is `i` not an object? It has a distinct lifetime, distinct memory, it's own name, and it has member functions (various operators). Defining `i` as not an object is just renaming it instead of actually looking at how it behaves. @hakre: A variable is just a named object. If you put `i` in a container, it's still an object- just like anything else. – DeadMG Aug 07 '11 at 15:27
  • @DeadMG: Thanks for the clarification, interesting indeed. But it's shifting a bit from the context of my question that was about class instance and object instance. Would you consider calling `i` an "Object Instance" or would this be needless doubling of words and you would go with either "Instance" or "Object"? – hakre Aug 07 '11 at 15:31
  • 3
    `i` is not an object, it's a *variable*, and whatever gets assigned to it is its *value*. A value *can* be an object, but isn't necessarily one. This is where the distinction between value and reference types comes in. Reference type instances (i.e. class instances) are objects. Value type instances are simply values, and they behave very differently from reference type instances (objects). – Aaronaught Aug 07 '11 at 15:49
  • 1
    "In what way is `i` not an object?" E.g. in the only way "object" is defined by Java specification (http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html): "An object (§4.3.1) is a dynamically created instance of a class type or a dynamically created array". For my own point of view, I agree with @Aaronaught. A variable (like `i`) is just a location where values may be stored. – Alexey Romanov Aug 07 '11 at 15:57
  • 1
    Actually, to be fair, I should qualify my comment: I'm referring the Object-Oriented definition, whereas before OO, the term "object" was more broadly applicable and described any addressable "thing" in a program. So if you're a COBOL or FORTRAN guy then I could understand, but most of us here are either OO (where "object" means "class instance") or FP (where "object" means something entirely different and sometimes nothing at all). – Aaronaught Aug 07 '11 at 15:57
  • 1
    @Aaronaught: Value and reference types in what language? The OP didn't specify any language, and the last time I checked OOP theory, it mentioned absolutely nothing to do with values, or references, or distinguishing between them. – DeadMG Aug 07 '11 at 15:59
  • @DeadMG So? This is still an answer to your question. Here is another: you can't send a message to `i` (again, depending on language; but in, e.g., Smalltalk, where you can send a message to an integer (again, an integer _value_ and not a _variable_), it _is_ an instance of a class). – Alexey Romanov Aug 07 '11 at 16:04
  • 2
    Yeah ok, if you're going to use that defense then you have to be a little more specific with your counter-example. You just wrote "int i" and didn't specify the language; in *most* languages that use that syntax (Java, C#, C++, D, etc.) "class instance" is exactly what "object" means. The only exception is C, and C doesn't have "classes" at all, so that doesn't appear to be the basis for your answer. – Aaronaught Aug 07 '11 at 16:06
  • 1
    @Aaronaught: It doesn't have to be any language. I can write a language where 1 + 1 = 3, but if I asked a question about it without tagging it, you'd assume that 1 + 1 = 2. Similarly, if you tag `class` and you don't tag a language, then it doesn't concern some language designer's view of what's practical in his language- it concerns OOP, and OOP alone, and the last time I checked, it makes absolutely no discriminations in this field. – DeadMG Aug 07 '11 at 16:09
  • @DeadMG: _You_ get to say what language it is, in which: 1) this syntax is used; 2) `i` is considered an object; 3) `i` is not an instance of a class. I'll grant you C (based on @Jerry Coffin's answer); any others? – Alexey Romanov Aug 07 '11 at 16:11
  • 3
    Look, you've given us a "disproof by counter-example" that (a) doesn't disprove anything and (b) isn't even a real example. You can't just invent your own definitions and justify it on the basis that the OP didn't specify a language. If you can find a *real* counterexample (as Jerry Coffin did and as I explicitly alluded to in my "I should qualify my comment" follow-up), great. Otherwise, you're not adding anything constructive – Aaronaught Aug 07 '11 at 16:12
  • 1
    Java invented their own definitions. I don't get to invent my own definitions? Are their definitions better because they're a bigger company with an implementation of a language using those definitions? @Alexey: The unfortunate irony is that I've just finished building a parser for said language :( – DeadMG Aug 07 '11 at 16:13
  • 1
    @DeadMG: When answering a question about normal usage of some term, you _don't_ get to invent your own definition for it. "Are their definitions better because they're a bigger company with an implementation of a language using those definitions?" No, they are better because they are widely used. – Alexey Romanov Aug 07 '11 at 16:23
  • @Alexey: That doesn't make them better at all. That just makes them more widely used. I never said that my definitions are great. They do, however, correlate with what OOP actually *says*, unlike Java's definitions, which absolutely does make them better. If the OP wants Java's opinion, he will tag Java, else, he refers to OOP in general. – DeadMG Aug 07 '11 at 19:09
  • 2
    @Aaronaught: You are simply wrong here. The question was whether an "object" is always an "instance of a class". It's not because, as DeadMG shows, in at least one language (I know two, actually) there are objects that are not instances of a class. What DeadMG says here is plain right, and saying it's wrong is plain wrong. (This is the moment where you need to remove your downvote, or you're considered unfair.) – sbi Aug 07 '11 at 19:10
  • 1
    @Alexey: I started to learn C++ in the early 90ies, and since then labeling instances of built-in types as "objects" is the "normal usage" for me (and for millions of other C and C++ programmers, some even longer). Later Java came, stole some ideas and a lot of syntax from C++, lots of idioms from C, dumbed it down to a restricted subset, and invented the funny idea that "objects" are only the instances of classes. That still seems silly to me, and when you ask in one of SO's most busy chat rooms (C++, busier than the Java room), they'll whack you over your head with "normal usage". _Upvoted!_ – sbi Aug 07 '11 at 19:16
  • 3
    @sbi: No, DeadMG hasn't shown *any* actual language. There is no language specified or even clearly implied, and the OP has refused to clarify. I'm fairly certain that the downvotes are for the utter lack of substance in this answer, and not necessarily because the vague point it's attempting to get across is actually wrong. You also don't know how or if I voted, and you have no business telling me or anybody else how to vote. – Aaronaught Aug 07 '11 at 19:16
  • @Aaronaught: OOP is not specific to a language, and the questioner also did not specify or even imply a language. – DeadMG Aug 07 '11 at 19:22
  • 1
    What's your point? OOP isn't even specific to this question. Your *answer* is specific to a language, or rather, it would *have* to be in order to make any sense. – Aaronaught Aug 07 '11 at 19:24
  • @Aaronaught: It does not have to be specific to one language. I know at least two where `i` as defined in the answer is considered an object. That proves the OP's statement (as cited in the answer) wrong and the answer correct. The OP didn't ask for any one language, so his quest for the right terms must be seen as _across all/common languages_. In at least two common languages `int i` is considered as defining the object `i`, while `int` is not a class, so not all objects are instances of classes. – sbi Aug 07 '11 at 19:31
  • @Aaronaught: It doesn't have to be specific to any language. OOP says that defining a variable that way is an object. – DeadMG Aug 07 '11 at 19:39
  • 1
    @sbi: There is one answer here which actually names a language and points to a specification in order to substantiate its claims, and that answer has several upvotes. This one does not substantiate itself at all, it includes an unidentified and hypothetical example as well as a logical fallacy. If you or DeadMG would care to provide the same level of detail/evidence as the upvoted answer then the discussion would be a lot more constructive. If you can't be bothered, then don't expect other people to listen to what you have to say. – Aaronaught Aug 07 '11 at 19:42
  • DeadMG, I'm tired of talking in circles with you. OOP says no such thing, and the fact that you're neither able to name any OOP language in which it applies nor point to any specification or other reliable source to back up the vague and largely meaningless example, even after all this protracted debate, is a pretty strong indicator that there *isn't* one. If you think your answer has merit then it should be no trouble at all to improve it; otherwise, this comment discussion is thoroughly pointless and unconstructive. – Aaronaught Aug 07 '11 at 19:46
  • @DeadMG: OOP as defined by Nygaard (http://c2.com/cgi/wiki?NygaardClassification): "A program execution is regarded as a physical model, simulating the behavior of either a real or imaginary part of the world." Nothing about whether `int i` is an object. OOP defined by Kay (http://c2.com/cgi/wiki?AlanKaysDefinitionOfObjectOriented): "OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late binding of all things" Strike against `int i` being an object: no hiding or late binding going on there! – Alexey Romanov Aug 07 '11 at 19:48
  • From definitions at http://c2.com/cgi/wiki?DefinitionsForOo which ones imply `int i` is an object? – Alexey Romanov Aug 07 '11 at 19:51
  • 1
    @Alexey: that's BS. If you use Alan Kay's definition, then nothing in Java can be considered an object either. Where in Java do you see "messaging" or "extreme late binding of all things"? Or could it be that, *gasp* the term "object" is somewhat overloaded and ambiguous and can mean any of a few dozen different things in different languages? As for `int`, where exactly do you see its internals leak out? I only see a very sensible interface: it exposes the functionality of an integer, and nothing else. That sounds pretty well encapsulated to me. – jalf Aug 07 '11 at 20:29
  • @jaif: "Or could it be that, gasp the term "object" is somewhat overloaded and ambiguous and can mean any of a few dozen different things in different languages?" Yes, that's why "OOP says that defining a variable that way is an object" without specifying which language and definiton of OOP you are talking about is wrong. Congratulations for getting my point. – Alexey Romanov Aug 07 '11 at 20:39
  • @Alexey: Smalltalk invented alienating built-ins as not being "objects" because they aren't of class-type?? Aren't you carried away a bit here? – sbi Aug 07 '11 at 20:49
  • @Aaronaught: So your gripe with this answer here is that it says _ is not true, because _ without naming the languages where __ applies?! This is why you're discussing here? – sbi Aug 07 '11 at 20:51
  • @sbi: No, but in Smalltalk all objects are instances of classes. Actually it seems that Simula 67 invented the idea you are talking about, still rather before C++ :) E.g. "we chose the terms "class" and "objects" of classes for our new Simula" (http://www-sst.informatik.tu-cottbus.de/~db/doc/People/Broy/Software-Pioneers/Dahl_new.pdf) – Alexey Romanov Aug 07 '11 at 21:30
  • Hi DeadMG, before we clean these comments up, is there anything from the feedback you've received here that you'd like to incorporate back into your answer? Otherwise, if everyone can take the extended discussion to [chat](http://chat.stackexchange.com/rooms/21), that'd be awesome. –  Aug 08 '11 at 00:29
  • i would be a variable of the type int, or an object of the class int. – TZubiri Nov 22 '18 at 08:11