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

What are instance variables and methods

  • 5 min

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 text fields Name and Id.

Persona
  • stringNombre
  • stringDni

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

variables-metodos-instancia

Each person has its independent variables

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

Instance Variables and Methods

Instance Variables

Instance variables, also called fields or attributes, 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 purpose, each 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 that same instance. They can also invoke other instance methods.

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

Practical Case

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

Persona
  • stringNombre
  • stringDni
  • stringSaludar()
  • stringMandarEmail()

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

Person luis = new Person();
luis.Name = "Luis";
luis.Id = "000000000";

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

Furthermore, we could use the Greet() method to print each instance’s Name variable to the screen.

luis.Greet()  // would display 'Luis' on screen
maria.Greet()  // would display 'Maria' on screen
Copied!

This is possible because instance methods have access to the variables and methods of that 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);
Copied!

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;  
Copied!

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);
Copied!

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);
Copied!

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)
Copied!