What is the difference between an Instance and an

2020-01-23 05:22发布

问题:

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

回答1:

An instance is an object in memory. Basically you create object and instantiate them when you are using them.

Here is a nice writeup on Classes Vs Objects Vs Instances, he is talking Java but it applies to all OO.

http://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/



回答2:

The Instance and Object are from Object Oriented Programming.

For the 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 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 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.



回答3:

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?



回答4:

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.



回答5:

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.



回答6:

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



回答7:

Objects and instances are mostly same; but having a very small difference. If Car is a class, 3 Cars are 3 different objects. All 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 the objects are instances. Not all the instances must be objects. Instances may be "structure instances" or "objects". I hope this will make the difference clear to you.



回答8:

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



回答9:

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.



回答10:

If we see the Definition of Object and Instance object -

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

Let us see the Definition of instance -

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

Now understand the meaning of any run time 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 run time memory allocation but here we call as instance so t1 is instance here we can not say t1 as object so Every object is the instance of Class but every Instance is not Object.



回答11:

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.



回答12:

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



回答13:

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


回答14:

each object said to be an instance of its class but each instance of the class has its own value for each attributes intances shares the attribute name and operation with their intances of class but an object contains an implicit reference to his on class



回答15:

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.



回答16:

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

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

Can also read the 'Inner classes' section of this java document on nested classes - https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html



回答17:

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.

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



回答18:

Quick and Simple Answer

  • Class : a specification, blueprint for an object
  • Object : physical presence of the class in memory
  • Instance : an unique copy of the object (same structure, different data)


回答19:

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.



回答20:

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



回答21:

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



回答22:

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.