What is the difference between an Instance and an Object?
Asked Answered
H

25

130

What is the difference between an Instance and an Object? Is there a difference or not?

Honolulu answered 21/5, 2010 at 20:36 Comment(3)
Maybe you can deduce from the well known error message "Object reference not set to an instance of an object." :->Balance
From JVM spec: "An object is either a dynamically allocated class instance or an array. " docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.htmlCarousel
StackOverFlow I have just given a brief description on difference between object and instance I hope it helpsWards
O
83

The Instance and Object are from Object Oriented Programming.

For some programming languages like Java, C++, and Smalltalk, it is important to describe and understand code. In other languages that used in Structured Programming, this concept doesn't exist.

This is a view from Structural Programming. There's no real significant difference that should consume too much of your time. There might be some fancy language that some people might take up a lot of spaces to write about, but at the end of the day, as far as a coder, developer, programmer, architect, is concerned, an instance of a class and an object mean the same thing and can often be used interchangeably. I have never met anyone in my career that would be picky and spend a half-hour trying to point out the differences because there's really none. Time can be better spent on other development efforts.

UPDATE With regards to Swift, this is what Apple who invented Swift prefers :

An instance of a class is traditionally known as an object. However, Swift classes and structures are much closer in functionality than in other languages, and much of this chapter describes functionality that can apply to instances of either a class or a structure type. Because of this, the more general term instance is used.

Orchestral answered 21/5, 2010 at 20:52 Comment(2)
What about languages that do not have classes but do have structs? Could I say "an instance of a struct" or would that be intrinsically wrong? How should I call it in that case? Thanks!Papule
It wouldn't necessarily be wrong, but it would sound awkward, like using British English terms like "lorry", "lift", and "chips" in the US instead of "truck", "elevator", and "fries".Periodontal
H
21

Excellent question.

I'll explain it in the simplest way possible: Say you have 5 apples in your basket. Each of those apples is an object of type Apple, which has some characteristics (i.e. big, round, grows on trees).

In programming terms, you can have a class called Apple, which has variables size:big, shape:round, habitat:grows on trees. To have 5 apples in your basket, you need to instantiate 5 apples. Apple apple1, Apple apple2, Apple apple3 etc....

Alternatively: Objects are the definitions of something, instances are the physical things.

Does this make sense?

Haver answered 21/5, 2010 at 20:51 Comment(2)
It doesn't. :) "Objects are the definitions of something" What you call objects here are classes.Agon
The analogy was fine up to the "Alternative" line. (Although I'd argue it's the tree that instantiates Apple to produce individual apples, not the person with the basket.)Periodontal
N
16

Instance: instance means just creating a reference(copy).

object: means when memory location is associated with the object (is a run-time entity of the class) by using the new operator.

In simple words, Instance refers to the copy of the object at a particular time whereas object refers to the memory address of the class.

Nystagmus answered 21/5, 2010 at 20:36 Comment(2)
saying that an instance is a reference to an object really cleared things up for me.Status
@JohnC It shouldn't, because it isn't correct. An object is an instance of a class. That's all there is to it.Operculum
V
12

Object:

It is a generice term basically it is a Software bundle that has state(variables) and behaviour(methods)

Class:

A blue print(template) for an object instance-it's a unique object thing for example you create a object two times what does that mean is yo have created two instances

Let me give an example

Class student()
{
   private string firstName;
  public student(string fname)
  {
    firstName=fname;
  }
  Public string GetFirstName()
  {
    return firstName;
  }
}

Object example:

Student s1=new student("Martin"); Student s2=new student("Kumar");

The s1,s2 are having object of class Student

Instance:

s1 and s2 are instances of object student the two are unique.

it can be called as reference also.

basically the s1 and s2 are variables that are assigned an object

Visional answered 22/3, 2013 at 15:33 Comment(1)
An object is an instance of a class.Operculum
G
10

Objects and instances are mostly same; but there is a very small difference. If Car is a class, 3 Cars are 3 different objects. All of these objects are instances. So these 3 cars are objects from instances of the Car class.

But the word "instance" can mean "structure instance" also. But object is only for classes.

All of the objects are instances. Not all of the instances must be objects. Instances may be "structure instances" or "objects". I hope this makes the difference clear to you.

Galvanize answered 1/3, 2016 at 10:0 Comment(0)
P
8

Let's say you're building some chairs.

The diagram that shows how to build a chair and put it together corresponds to a software class.

Let's say you build five chairs according to the pattern in that diagram. Likewise, you could construct five software objects according to the pattern in a class.

Each chair has a unique number burned into the bottom of the seat to identify each specific chair. Chair 3 is one instance of a chair pattern. Likewise, memory location 3 can contain one instance of a software pattern.

So, an instance (chair 3) is a single unique, specific manifestation of a chair pattern.

Palaver answered 21/5, 2010 at 21:3 Comment(2)
An object is an instance of a class. An instance isn't an instance of an object. Answer is meaningless.Operculum
Wow, after nine years someone finally pointed out the flaw in my explanation. Thanks! Better now?Palaver
R
6

Quick and Simple Answer

  • Class : a specification, blueprint for an object...
  • Object : physical presence of the class in memory...
  • Instance : a unique copy of the object (same structure, different data)...
Reredos answered 17/5, 2019 at 4:38 Comment(0)
O
4

An object is a construct, something static that has certain features and traits, such as properties and methods, it can be anything (a string, a usercontrol, etc)

An instance is a unique copy of that object that you can use and do things with.

Imagine a product like a computer.

THE xw6400 workstation is an object

YOUR xw6400 workstation, (or YOUR WIFE's xw6400 workstation) is an instance of the xw6400 workstation object

Ordovician answered 21/5, 2010 at 20:41 Comment(1)
Your first and second paragraphs describe classes, not objects.Operculum
S
4

Java is an object-oriented programming language (OOP). This means, that everything in Java, except of the primitive types is an object.

Now, Java objects are similar to real-world objects. For example we can create a car object in Java, which will have properties like current speed and color; and behavior like: accelerate and park.

That's Object.

enter image description here

Instance, on the other side, is a uniquely initialized copy of that object that looks like Car car = new Car().

Check it out to learn more about Java classes and object

Saundrasaunter answered 14/4, 2019 at 15:49 Comment(0)
A
2

Once you instantiate a class (using new), that instantiated thing becomes an object. An object is something that can adhere to encapsulation, polymorphism, and abstraction principles of object oriented programming and the real thing a program interacts with to consume the instance members defined in the class. The object contains instance members (non-static members).

Thus, an instance of a class is an object. The word ‘instance’ is used when you are referring to the origin from where it was born, it's clearer if you say ‘instance of a class’ compared to ‘object of a class’ (although the latter can be used too).

Can also read the 'Inner classes' section of this java document on nested classes -

https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Adornment answered 12/1, 2019 at 6:10 Comment(0)
H
2

I can't believe, except for one guy no one has used the code to explain this, let me give it a shot too!

// Design Class
class HumanClass {
    var name:String
    init(name:String) {
        self.name = name
    }
}

var humanClassObject1 = HumanClass(name: "Rehan") 

Now the left side i.e: "humanClassObject1" is the object and the right side i.e: HumanClass(name: "Rehan") is the instance of this object.

var humanClassObject2 = HumanClass(name: "Ahmad") // again object on left and it's instance on the right.

So basically, instance contains the specific values for that object and objects contains the memory location (at run-time).

Remember the famous statement "object reference not set to an instance of an object", this means that non-initialised objects don't have any instance. In some programming languages like swift the compiler will not allow you to even design a class that don't have any way to initialise all it's members (variable eg: name, age e.t.c), but in some language you are allowed to do this:

// Design Class
class HumanClass {
    var name:String // See we don't have any way to initialise name property.
}

And the error will only be shown at run time when you try to do something like this:

var myClass = HumanClass()
print(myClass.name) // will give, object reference not set to an instance of the object.

This error indicates that, the specific values (for variables\property) is the "INSTANCE" as i tried to explain this above! And the object i.e: "myClass" contains the memory location (at run-time).

Hypodermis answered 30/11, 2019 at 22:12 Comment(0)
S
2

This answer may be seen as trite, but worrying about the differences between an instance and object is already trite city.

I think its best depicted in javascript:

let obj= {"poo":1} 
// "obj" is an object

verses

Class Trash {
    constructor(){this.poo = 1;}
}

let i = new Trash();
// "i" is an instance
Shores answered 12/12, 2019 at 18:1 Comment(0)
F
2

When a variable is declared of a custom type (class), only a reference is created, which is called an object. At this stage, no memory is allocated to this object. It acts just as a pointer (to the location where the object will be stored in future). This process is called 'Declaration'.

Employee e; // e is an object

On the other hand, when a variable of custom type is declared using the new operator, which allocates memory in heap to this object and returns the reference to the allocated memory. This object which is now termed as instance. This process is called 'Instantiation'.

Employee e = new Employee(); // e is an instance

However, in some languages such as Java, an object is equivalent to an instance, as evident from the line written in Oracle's documentation on Java:

Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.

Fbi answered 21/3, 2020 at 13:47 Comment(5)
This is just an example that looks intuitive, but wrong. In Employee e, e points to nowhere and does not exist. While in theory object is some real entity that exists out of blue print (Class).Wrestle
@ManishSoni What you are speaking of is an 'instance' unless you are speaking in context of languages which consider an 'object' and an 'instance' as synonyms! That's what the entire answer just explains.Fbi
The example here is misleading, it is synonymous with Java syntax where this does not make sense. However, your answer conveys the same meaning (conceptually right) this is what I told 'it looks intuitive but wrong'.Wrestle
@ManishSoni The example is not synonymous to only Java. If you look at other languages (C++, C#, JavaScript, etc.), many of them use the new operator and classes. Don't look at the example keeping Java or any language's syntax in mind. The example is language-agnostic.Fbi
"When a variable is declared of a custom type (class), only a reference is created, which is called an object." :- Do you think this statement is right? reference is called object? Your elaboration for the answer made this debatable. There is one concise straight answer. why complicate and confuse this?Wrestle
C
1

An instance is a specific representation of an object. An object is a generic thing while an instance is a single object that has been created in memory. Usually an instance will have values assigned to it's properties that differentiates it from other instances of the type of object.

Clericalism answered 21/5, 2010 at 20:42 Comment(0)
S
1

If we see the Definition of Object and Instance object -

Memory allocated for the member of the class at run time is called object or the object is the instance of the Class.

Let us see the Definition of instance -

Memory allocated For Any at run time is called an instance variable.

Now understand the meaning of any runtime memory allocation happen in C also through Malloc, Calloc, Realloc such:

struct p
{

}
p *t1
t1=(p) malloc(sizeof(p))

So here also we are allocating runtime memory allocation, but here we call as an instance so t1 is an instance here we can not say t1 as an object, so Every object is the instance of Class, but every Instance is not an Object.

Spang answered 10/9, 2014 at 10:37 Comment(1)
There's a correct answer lurking in here somewhere but the language needs cleaning up and simplifying.Operculum
O
1

Object - An instance of a class that has its own state and access to all of the behaviour defined by its class.

Instance - Reference to an memory area for that particular class.

Organelle answered 20/10, 2014 at 11:10 Comment(0)
A
1

Class : A class is a blue print. Object : It is the copy of the class. Instance : Its a variable which is used to hold memory address of the object.

A very basic analytical example

Class House --> Blueprint of the house. But you can't live in the blue print. You need a physical House which is the instance of the class to live in. i.e., actual address of the object is instance. Instances represent objects.

Aiglet answered 18/1, 2017 at 19:41 Comment(1)
Instance refers to the object, not to a variable.Operculum
R
1

There are 3 things you need to understand : Class , Object and Instance.

Class : Class is the blueprint code from which you will create an Object(s)

Object : When memory is allocated to the data entity (created from blueprint class) , that data entity or reference to it is called Object

Instance : When data is filled in an Object , it becomes an instance of that Object. It can also be called a state of that Object.

Example : In context with C# (objects are reference type here)

Lets say we have a class like this (This is your blueprint code)

public class Animal
{
//some fields and methods     
}

We create an object like this

Animal a = new Animal();
Animal b = a;
Animal c = a;
Animal d = b;

So here is the question : How many objects and instances are here ?

Answer : There is only 1 object but 4 instances.

Why ?

In first line (Animal a = new Animal();),we created an Object from class Animal with new Operator. That Object is somewhere on your RAM. And the reference to that Object is in "a". We have 1 object and 1 instance at this time.

Now in next line, we assign b with a. Here Object is not copied but the reference of object from "a" is stored in "b" too. Thus , we have 2 instances , "a and b".

This goes on and we only copy reference of same object located at some memory.

Finally , we have 4 instances "a,b,c,d" of a single object that was created with new Operator.

(Read how reference type works in C# for more. I hope you understand my language)

Riffe answered 14/5, 2021 at 14:38 Comment(0)
S
0

each object said to be an instance of its class, but each instance of the class has its own value for each attribute instances share the attribute name and operation with their instances of class but an object contains an implicit reference to its own class

Steinberg answered 5/9, 2013 at 11:39 Comment(0)
R
0

I can't believe this could be hard to explain, but it is actually easier than all the answers I read. It is just simple like this.

Firstly, you need to understand the definition:

Instance is a **unique copy-product of an Object.

**unique - have different characteristics but share the same class compare to object

Object is a name that has been used to keep the Class information (i.e method)

Let's say, there is a toy_1 as an object. There is also toy_2 as an object ----> which is ALSO an INSTANCE to toy_1. At the same time, toy_1 is also an INSTANCE to toy_2. (remember again INSTANCE is a COPY-PRODUCT)

That is why most of the answers I found said it is INTERCHANGABLE. Thank you.

Redbreast answered 26/8, 2021 at 15:11 Comment(0)
G
0

I think if we consider other approaches than OOP (mainly by assuming the term Class hasn't always been used, as it's the case for many C projects, which still applied the concept of Objects), following definitions would make the most sense:

A Class defines an interface that objects adhere to.

An Object is an aggregate of different fields. (It doesn't have to "physically" exist, but it can).

All Objects of the same Class can be used in the same way, defined by the Class.

An Instance is a unique realization of an Object.

As many OOP languages use static typing, the Object description is usually part of the Class already. As such, when talking about an Object in C/C++, what usually is meant is the Instance of an Object. In languages that do not have static typing (such as JavaScript), Objects can have different fields, while still sharing the same Class.

Gotama answered 15/12, 2021 at 12:35 Comment(0)
H
-1

Regarding the difference between an object and an instance, I do not think there is any consensus.

It looks to me like people change it pretty much interchangeably, in papers, blog posts, books or conversations.

As for me, the way I see it is, an object is a generic and alive entity in the memory, specified by the language it is used in. Just like the Object class in Java. We do not much care its type, or anything else associated with it, whether it is managed by a container or not.

An instance is an object but associated with a type, as in this method accepts Foo instances, or you can not put Animal instances in an instance of a List of Vehicles.

objects for example have locks associated with them, not instances, whereas instances have methods. objects are garbage collected, not instances.

But as I said, this is only how I see it, and I do not think there is any organisation we can refer to for a standard definition between them and everyone will pretty much have their slightly different understanding / definitions (of course within limits).

Heading answered 26/1, 2019 at 15:47 Comment(0)
P
-2

An object is a generic thing, for example, take a linear function in maths

ax+b is an object, While 3x+2 is an instance of that object

Object<<< Instance

General<<< Specific

There is nothing more to this

Poor answered 29/5, 2019 at 15:23 Comment(0)
F
-3

An object can be a class, say you have a class called basketball.

but you want to have multiple basketballs so in your code you create more than 1 basketball

say basketball1 and basketball2. Then you run your application. You now have 2 instances of the object basketball.

Filing answered 21/5, 2010 at 20:42 Comment(3)
That would mean a Object is a Instance of a Class Constructor ?Honolulu
think of the object like a blueprint. say we have a blueprint for the basketball, that is the class. when the basketball is created and made it now exists, so that would mean we have 1 instance of the object basketball. if we built another basketball from the object(blueprint). we now have 2 instances of the basketball. There is always just 1 object, but we can make many instances of that object.Filing
@Darxval then what is a classPoor
C
-4

Object refers to class and instance refers to an object.In other words instance is a copy of an object with particular values in it.

Coinage answered 20/3, 2017 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.