What is the difference between an Instance and an

2020-01-23 04:44发布

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

22条回答
狗以群分
2楼-- · 2020-01-23 05:06

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

查看更多
虎瘦雄心在
3楼-- · 2020-01-23 05:07

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

查看更多
萌系小妹纸
4楼-- · 2020-01-23 05:08

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/

查看更多
祖国的老花朵
5楼-- · 2020-01-23 05:10

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?

查看更多
Luminary・发光体
6楼-- · 2020-01-23 05:11

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

查看更多
倾城 Initia
7楼-- · 2020-01-23 05:13

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

查看更多
登录 后发表回答