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

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,
- 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";
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
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);
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 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)
