what is meaning of instance in programming?

68,534

Solution 1

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

Solution 2

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.

Solution 3

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?

Solution 4

int main()
{
    int a;     //An instance of integer
    int a,b;   //two instances of integer
    struct1 a; //An instance of struct1
    return 0;
}

Solution 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

Share:
68,534
user3057928
Author by

user3057928

Updated on December 10, 2021

Comments

  • user3057928
    user3057928 over 2 years

    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.