Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is meaning of instance in programming?

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.

like image 951
user3057928 Avatar asked Dec 09 '13 02:12

user3057928


People also ask

What is instance in programming example?

In programming, an instance is one occurrence of a class or object. For example, a program may have a class/object named Animal, but there could be many instances of Animal, such as lion, cat, and dog. An example using JavaScript is shown below where the Animal object is created, followed by three instances.

What do you mean of instance?

a particular situation, event, or fact, especially an example of something that happens generally: There have been several instances of violence at the school.

What is an instance in technology?

In object-oriented programming (OOP), an instance is a concrete occurrence of any object, existing usually during the runtime of a computer program.

What is the meaning of instance in C++?

An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks. Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class. Access modifiers can be given to the instance variable.


2 Answers

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

like image 92
broofa Avatar answered Oct 12 '22 18:10

broofa


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.

like image 19
Aukins Moruri Avatar answered Oct 12 '22 17:10

Aukins Moruri