37

I don't get it for long. Are there any alternative words similar to 'instance' that are easier to understand? For a non-programmer, how you explain instance? Instance is like example in normal person's world. I can't understand what it is if I don't even understand its meaning.

openshac
  • 4,966
  • 5
  • 46
  • 77
user3057928
  • 605
  • 1
  • 9
  • 12

10 Answers10

43

"instance" is best understood as it relates to "class" in programming. "Classes" are used to define the properties and behavior of a category of things. E.g. A "Car" class might dictate that all cars be defined by their make, model, year, and mileage.

But you can't provide specifics about a particular car (for example, that 1978 Chevy Impala with 205,000 miles on it that your uncle Mickey drives) until you create an "instance" of a Car. It's the instance that captures the detailed information about one particular Car.

broofa
  • 37,461
  • 11
  • 73
  • 73
  • Had not read this when i wrote my answer - I just realized you and I were saying similar things... – Floris Dec 09 '13 at 02:36
  • 1
    it sound so similar to object now.. correct? why don't they say object but instance? – user3057928 Dec 09 '13 at 02:52
  • 4
    "object" and "instance" are often used interchangeably in casual discussion between programmers. I suspect the reason for this is that most OO languages have an "Object" class at the top of the class-hierarchy. Instances of that base class are, naturally, called "objects". And because all classes inherit from the base class, all instances will have that minimum "object" behavior. Thus, the blurring of definition between "object" and "instance". – broofa Dec 09 '13 at 14:29
  • Also, it's worth noting that in Javascript where "everything is an object", objects have a bit more behavior than in other languages. Specifically, the ability to hang arbitrary key-value pairs off of pretty much any object is somewhat unusual. In other languages, that behavior is encapsulated in "map", "hash", or "dictionary" classes (a whole other set of terms that may be used interchangeably, btw) – broofa Dec 09 '13 at 14:34
21

To understand what an instance is, we must first understand what a class is.

A class is simply a modeling tool provided by a programming language for use in representing real world objects in a program or application.

The class is structured to accommodate an object's properties (member variables) and its operations (member functions/methods).

An Instance on the other hand is simply a variation of an object created from a class. You create an object variant (Instance) using a constructor which is a method within a class specifically defined for this purpose.

Consider a Car, if you wanted to represent it in your application you would define a class identified as Car which contains the properties of the car and the operations that the car can perform.

It would look something close to this, supposing it was done in Java programming language:-

public class Car{
    //the properties of the car
    private String make;
    private int year;
    private int gear;
    private int speed;
    ...

    //constructor used to create instances of the car
    public Car(String carMake, int yearManf){
        year = yearManf;
        make = carMake;
    }

    //Car Operation/methods

    public void setGear(int gearValue){
        gear = gearValue
    }
    public void applyBrake(int decrement){
        speed -= decrement;
    }
    public void accelerate(int increment){
        speed += increment;
    }   
    ...
}

Create an instance of a car:-

Car BMW = new Car("385 i", 2010);

BMW here is an instance of a car.

Aukins Moruri
  • 339
  • 2
  • 4
11

Going outside the world of programming for a second: you know what people are. You are an "instance" of the class "people" - I can talk about people in general (the class of objects), or if I have a specific one in mind, I talk of an "instance". An instance can have properties that are not automatically a consequence of being a member of the class. All humans have a heart, but not all humans have your name and date of birth.

I hope that clears it up a bit?

Floris
  • 45,857
  • 6
  • 70
  • 122
  • is it like object? I believed it is. But why don't use 'object' to say things? lol – user3057928 Dec 09 '13 at 02:57
  • 1
    "Instance" is one object of a particular class - so when you say "instance" you usually do so in the context of "the type (or class) of object". You are an instance of the class people. Subtle difference. – Floris Dec 09 '13 at 12:03
6
int main()
{
    int a;     //An instance of integer
    int a,b;   //two instances of integer
    struct1 a; //An instance of struct1
    return 0;
}
JMercer
  • 352
  • 2
  • 9
  • aren't they are variable? lulz – user3057928 Dec 09 '13 at 02:20
  • Yes but you're saying "Here's a variable; let's call it a, it has an integer type. We're going to store a value in it.". Saying "An instance of an integer, a." is the same thing:) – JMercer Dec 09 '13 at 02:27
5

Here is a pretty standard definition:

An instance, in object-oriented programming (OOP), is a specific realization of any object. An object may be varied in a number of ways. Each realized variation of that object is an instance. The creation of a realized instance is called instantiation.

Each time a program runs, it is an instance of that program. In languages that create objects from classes, an object is an instantiation of a class. That is, it is a member of a given class that has specified values rather than variables. In a non-programming context, you could think of "dog" as a class and your particular dog as an instance of that class.

http://whatis.techtarget.com/definition/instance

Here is a good conversation about instances that may help you out: https://softwareengineering.stackexchange.com/questions/99202/is-it-called-class-or-object-instance

Community
  • 1
  • 1
Willy2414
  • 119
  • 1
  • 8
2

An object from an object or reference from an object.

Kentot
  • 512
  • 5
  • 10
  • 26
2

Loosely speaking, there are patterns for making things and there are instances of those patterns.

A class is a pattern for creating objects. The objects that are created with it are instances of the class.

class C { };
C c;          // instance of C
C d;          // instance of C

A function template is a pattern for creating functions. The functions that are created with it are instances of the template. This is usually done implicitly, and referred to as "implicit instantiation".

template <class T> void f(T) { }
f(int);       // implicit instantiation of f<int>
f(double);    // implicit instantiation of f<double>

A class template is a pattern for creating classes. The classes that are created with it are instances of the template.

template <class T> class X { };
X<int> xi;    // X<int> is instance of X, xi is instance of X<int>
X<double> xd; // X<double> is instance of X, xd is instance of X<double>
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
1

An instance is basically an object. In actual english, it can mean differently. In this case instance in english can mean 'To Refer' or 'Reference'. These objects instances in programming are also a reference to the source code.

iWumbo
  • 135
  • 1
  • 8
1

with a simple example : we have a blueprint (class) represents student (object) with fields like name, age, course (class member). And we have 2 students here, Foo and Bob. So, Foo and Bob is 2 different instances of the class (Student class) that represent object (Student people).
credit: Alfred’s Computing Weblog

as far i have understood instance a pointer to object of class.

ps: i could be wrong.

naruto
  • 11
  • 1
1

Instance is a variable that holds the memory address of the Object.

Trump
  • 35
  • 5