Language: EN

que-son-metodos-variables-instancia-en-programacion

What are instance variables and methods

Instance variables and methods are elements that belong specifically to each particular instance created from a class.

In other words, each instance we create from a class will have its own unique and independent copy of these variables and methods.

For example, if we have a Person class, which contains two texts Name and Dni.

Person
  • stringName
  • stringDni

Each instance of a person we create with this class will have its own Name and Dni. They will be theirs, unique and independent from all the others.

variables-metodos-instancia

Each person has its own independent variables

Instance variables and methods are the “normal” ones, the ones we will use almost every day. In contrast, we have class variables and methods (or static ones) that we will see in the next article.

Instance variables and methods

Instance variables

Instance variables, also called fields or characteristics, are used to store the state of an instance.

Each object created from the class will have its own copy of these variables. For this, each one will have its own space in memory to store the information.

Instance methods

Instance methods are functions that act on an instance. These can access and manipulate the variables of the same instance. In addition, they can invoke other instance methods.

On the other hand, unlike instance variables, instance methods do not occupy memory for each instance. In general, the compiler will replace it with a single function that receives a reference to the instance.

Practical case

Let’s take our Person class again, which now contains two variables Name and Dni, as well as two methods Greet() or SendEmail() . That is,

Person
  • stringName
  • stringDni
  • stringGreet()
  • stringSendEmail()

Now, we create two instances of the Person class, which we will call john and mary. Each of them has different values for the variables Name and Age.

Person john = new Person();
john.Name = "John";
john.Dni = "000000000";

Person mary = new Person();
mary.Name = "Mary";
mary.Dni = "000000001";

Also, we could use the Greet() method to print the Name variable of each instance on the screen.

john.Greet()  // would display 'John' on the screen
mary.Greet()  // would display 'Mary' on the screen

This is possible because instance methods have access to the variables and methods of this instance.

Examples in different languages

Finally, let’s see how different languages implement instance variables and methods.

In C#, instance variables and instance methods belong to the individual instances of the class.

public class Counter
{
    // Instance variable
    public int Counter;

    // Instance method
    public void IncrementCounter()
    {
        Counter++;
    }
}

// Usage
var counter = new Counter();
counter.IncrementCounter();
Console.WriteLine(counter.Counter);

In C++, instance variables and instance methods belong to the individual instances of the class.

#include <iostream>

class Counter {
public:
    // Instance variable
    int Counter = 0;

    // Instance method
    void IncrementCounter() {
        Counter++;
    }
};

// Usage
Counter counter;
counter.IncrementCounter();
std::cout << counter.Counter << std::endl;  

In JavaScript, instance variables and instance methods are defined inside the class constructor using this.

class Counter {
    // Instance variable
    constructor() {
        this.counter = 0;
    }

    // Instance method
    incrementCounter() {
        this.counter++;
    }
}

// Usage
const counter = new Counter();
counter.incrementCounter();
console.log(counter.counter);

In TypeScript, instance variables and instance methods are similar to JavaScript.

class Counter {
    // Instance variable
    counter: number = 0;

    // Instance method
    incrementCounter(): void {
        this.counter++;
    }
}

// Usage
const counter = new Counter();
counter.incrementCounter();
console.log(counter.counter);

In Python, instance variables and instance methods are defined inside the class using self.

class Counter:
    def __init__(self):
        # Instance variable
        self.counter = 0

    # Instance method
    def increment_counter(self):
        self.counter += 1

# Usage
counter = Counter()
counter.increment_counter()
print(counter.counter)