Language: EN

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

What are instance variables and methods

Instance variables and methods are elements that specifically belong 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 class Person, which contains two texts Name and Id.

Person
  • stringName
  • stringId

Each instance of person that we create with this class will have its own Name and Id. They will be theirs, own, and independent of all others.

variables-metodos-instancia

Each person has their independent variables

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

Instance Variables and Methods

Instance Variables

Instance variables, also called fields or properties, 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 memory space to store the information.

Instance Methods

Instance methods are functions that act upon an instance. They can access and manipulate the variables of the same instance. They can also invoke other instance methods.

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

Practical Case

Let’s go back to our class Person, which now contains two variables Name and Id, as well as two methods Greet() or SendEmail(). That is,

Person
  • stringName
  • stringId
  • stringGreet()
  • stringSendEmail()

Now, we create two instances of the class Person, which we will call juan and maria. Each of them has different values for the variables Name and Id.

Person juan = new Person();
juan.Name = "Juan";
juan.Id = "000000000";

Person maria = new Person();
maria.Name = "Maria";
maria.Id = "000000001";

We could also use the method Greet() to print the Name variable of each instance.

juan.Greet()  // would display 'Juan' on screen
maria.Greet()  // would display 'Maria' on 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 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 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 within 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 within 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)