I have seen the phrases "instantiate an object", "instantiate a Square object", or "a Cat object is instantiated." (p.17, p.22, and p.26 of The Object-Oriented Thought Process, 4th Edition, Addison-Wesley).

And then when reading Design Patterns (the Gof book), I keep on reading "instantiate a class", as in "it must instantiate a Glyph subclass" or "A class creational pattern uses inheritance to vary the class that's instantiated" (p.48 and p.81, Design Patterns, Erich Gamma et al, Addison-Wesley).

But then, in the same book, it says, "A dashed arrowhead line indicates a class that instantiates objects of another class" (p.15) to mean "CreationTool creates LineShape objects" (p.364).

Do they mean the same thing? And if they do, which way of saying it is more correct or accurate? Is it true that in normal circumstances, we don't use running code to "create" a new class? (That is, "instantiate a class" doesn't mean create a new class).

I bring this up because while reading the Design Patterns book by GoF, I read "instantiate an object" and I thought, "ok, an object is created", then in the factory method chapter, I read "instantiate a class", and I thought "hmmm... a class is created?" and it kind of made me dizzy...

  • 2
    What is an object an instance of? – Oded Dec 30 '15 at 16:50
  • Code generation tools may well create new classes in my experience. – JB King Dec 30 '15 at 16:56
  • 2
    @DavidPacker Funny, I was thinking the exact opposite... You can't really instantiate a class, in the same way you can't construct a blueprint or template. You instantiate an object based on a class, the same (metaphorical) way you construct a building based on its blueprint. – Eric King Dec 30 '15 at 17:00
  • 3
    @太極者無極而生 For the record, I think both ways of phrasing are fine in that they both convey the same idea and I doubt anybody would be confused by either. But in my opinion, objects are instances, so objects are what get instantiated, not classes. – Eric King Dec 30 '15 at 17:07
  • 1
    @EricKing I indeed think your approach is the correct one. AFAIK instantiation is the process of creating something, you create an object, not a class itself. Classes are defined, they do not act (well in an ideal world where no static methods are). There is probably a reason the paradigm is called Object Oriented Programming and not Class Oriented Programming. – Andy Dec 30 '15 at 17:09

Both are correct and both mean the exact same thing. Objects are instances of classes. Whether you "instantiate class X [to create an object of class X]" or "instantiate an object [of class X]" is just semantic nitpicking.

  • 2
    +1 - the language here is meant to be communicated as understood, both statements are understood in the same way by anyone working in OOP, so there's no reason to bother over which is "better". – Jimmy Hoffa Dec 30 '15 at 17:45
  • 1
    -1 - While I find it hard to disagree with this answer factually, it's not an answer to this question. "You don't need an answer" is not the same as "<answer>". Besides, out-of-hand rejection of someone trying to determine a rigourous and precise semantics for a term seems like a backwards step. – Lightness Races with Monica Dec 30 '15 at 18:06
  • Although I would clarify it by mentioning that objects are instances of classes in some languages (e.g., Java, C++). – Mike Harris Dec 30 '15 at 19:04
  • 1
    In C++ objects are not instances of classes. Some objects are instances of classes. – DeadMG Dec 30 '15 at 20:31
  • @LightnessRacesinOrbit You should try to define precise semantics for a term- the answerer's provided definition is more than sufficiently precise. – DeadMG Dec 30 '15 at 20:31

Is it more accurate to say "instantiate a class" or "instantiate an object"?

Webster defines instantiate:

to represent (an abstraction) by a concrete instance

From this definition, we could use either "instantiate a class" or "instantiate an object." We could also simply say, "instantiate" since "class," (the abstraction) and, "object," (the concrete instance) are both now made redundant by the definition of instantiate.

If we wanted to be more precise, we could specify the object we're instantiating, for example: "instantiate a Foo."

Background information:

This chart demonstrates that the word "instantiate" did not come into common use until starting in the 1950s, and peaking very recently. Source.

frequency graph of "instantiate" over time, demonstrates it doesn't come in to useage until the late 1900s

Further, this ngram demonstrates that "instantiate an object" is much more common than "instantiate a class".

instantiate an object versus instantiate a class

  • 1
    If you include the phrases "instantiate a class" and "instantiate an object", you'll notice the 'object' variant is much more common. – Eric King Dec 30 '15 at 20:23
  • @EricKing added the chart. :) – Aaron Hall Dec 30 '15 at 23:02
  • Does anyone know what is the red and what is the green? – Mike Nakis Dec 31 '15 at 6:50
  • For some reason Google differentiates between capitalized and all lowercase. – Aaron Hall Dec 31 '15 at 13:48

To instantiate something means to provide a real instance of an abstract concept. The class is the abstract concept. An object is an instance of a class. When you create a new object, you're instantiating the class.

If you see "instantiate a Square object", that's usually shorthand for "create a new object by instantiating the Square class". It's not 100% correct but it conveys the same meaning.

"Instantiate" and "create" don't have the same meaning, so "creating a class" has a different meaning to "instantiating a class".


Is it more accurate to say "instantiate a class" or "instantiate an object"?

Here's an answer by analogy:

If a tailor has a pattern for a shirt that is an Oxford style shirt, would you say, "he makes a pattern," or, "he makes a shirt," or "he makes an Oxford?" "He makes a pattern" seems a bit off, because he's not actually making a pattern from which he can then make other shirts. It would be most precise to say that "He makes an Oxford," especially if, by the context, you understand that he's making a shirt.

Similarly, it's a bit off to say, "instantiate a class." It is an object that is instantiated. You are instantiating an object from the class as it is defined. It will then be an object of that type, say an Oxford, to continue with the analogy. However, from context, we do understand what is meant, and internally expand, "instantiate a class," to, "instantiate an object of class Oxford." The intent is clear from the context.

It's accurate to say "instantiate an object" because that's what is instantiated, an object. But you may as well say "instantiate a thing", it would be just as descriptive, it's just that "thing" has little to no usage in the context of programming, so this is a bit imprecise. So this is more accurate than saying "instantiate a class".

It's both accurate and precise to say "instantiate an Oxford" which would be a specific type of object. It's also more descriptive and informative - assuming you know that an Oxford is an object (in this case, a shirt).

If the intent is clarity of communication, I would aim to be both accurate and precise.

Not the answer you're looking for? Browse other questions tagged or ask your own question.