I've been teaching myself Object Oriented Programming(Ruby) for a while. However, I still don't quite understand some of its core principles, specifically, instance variable. Could somebody please explain to me why do we need instance variable? What is the problem that it is trying to solve in object oriented programming? When do I need to create one, and when not? I appreciate it.

  • 1
    Answer + example stackoverflow.com/questions/16686488/… Commented Jan 25, 2017 at 11:17
  • 5
    Smells like a homework question. Commented Jan 25, 2017 at 12:31
  • @MichelHenrich First of all, I am rails dev currently. So, homework doesn't make sense here. Secondly, I know there are many tutorials/explaination on the internet about the subject, but I still don't get the concept. Therefore, I decided to ask it here with the hope that somebody can make it clear to me. Just because somebody asks a why question to try to understand a particular concept doesn't mean that it is homework question. But anyway, everybody is free to smell whatever they like on the internet nowadays. ;)
    – Fatima
    Commented Jan 25, 2017 at 17:33
  • 2
    @ĐỗTiến Fair enough. I wrote that because we get homework questions more often than honest questions such as yours. In any case, there are good answers here already. Commented Jan 25, 2017 at 18:00

5 Answers 5

  • Instance variables are variables declare inside a class but outside any methods.
  • They can be accessed by all dynamic method of a class.
  • As opossed to a class variable which occurs only one for all instances of a class, with instance variables every instance have its own copy of them.
  • Instance variables represents the "state" of an object, meaning the values or properties that object holds in any given time.
  • With instance variables it's possible to have multiple instances of the Person class, each one having a different name attribute, i.e. instance1's name attribute can have the value "Joe" while instance2's name can be "Mary" and instance3's can be "Peter", because each one of the three instances have a copy of the instance variable name.
  • When you modify the value, such value is only changed for one instance. All others instances are not affected.
  • At this point you can figure that having different values for different instances is very useful in many programs.
  • 2
    +1 for Instance variables represents the "state" of an object.
    – Machado
    Commented Jan 25, 2017 at 11:39
  • 2
    "Instance variables are variables declare inside a class but outside any methods." – Huh? Instance variables don't have anything to do with classes. They have to do with objects (aka instances), that's why they are called instance variables. For example, in Scala, you can also declare instance variables in objects or in traits, not just in classes. Self doesn't even have classes, yet it has instance variables. In Ruby, which the OP explicitly mentioned, using an instance variable outside of a method inside of a class body means the instance variable belongs to the class, not instances of it. Commented Jan 25, 2017 at 12:08
  • 3
    @JörgWMittag As someone who uses C++, Java and C#, it makes perfect sense to me. In those languages, the only place you can declare things is in the class. There is no mechanism for declaring things in objects - those languages are statically typed, so you can't add anything that wasn't in the class.
    – Simon B
    Commented Jan 25, 2017 at 13:31
  • This answer makes perfect sense to someone who already understands the concepts. Commented Jan 25, 2017 at 19:21
  • @SimonB: The mentioned Scala is also statically typed and it allows to define instance variables directly in an object, without defining a class. C++, Java and C# do not represent all possible flavours of object-oriented programming.
    – Giorgio
    Commented Jan 25, 2017 at 21:12

There are several different purposes of classes in OOP, but one of the most common is to act as a blueprint for creating objects. An instance variable is part of the blueprint for creating an object. It defines a datum that does not exist initially, but every time you create an object of that class's type (or subclass), that object will get its own (usually private) variable with that name inside of it.

This is really the entire point behind OOP. Instead of working with data directly, we package data with methods that know how to manipulate the data. This makes it much, much easier for the programmer to interface to the data:

  • you only need to understand the interface/specifications to use the data correctly, and the interface is much, much simpler than the code
  • if the code changes but still obeys the specs, then anything using the code will still work (as long as the client code makes no assumptions outside of what the specification says)
  • it allows you to work on dependent objects at the same time (which makes writing large systems possible)
  • it allows you to switch out one component for another with the same specs, even dynamically (while the program is running), and code reliant on the interface which bears those specs will work for both cases (this is where polymorphism comes from)

All of these are pretty much essential for writing large software. That's why we make these blueprints called classes.

  • Thanks for your answer, but could you give me a specific example why dealing directly data is not a good decision?
    – Fatima
    Commented Jan 26, 2017 at 6:00
  • 1
    @ĐỗTiến I gave you four examples. But think of it this way. A car has a lot of complex stuff under the hood, but a simple interface (steering, brake, gas). Why don't we have knobs to control the engine and timing belt? We'd ruin the car. We wouldn't be able to drive other cars. We'd need an engineering degree just to drive. These same things apply to complex components in SE. The code that uses a Car object shouldn't need to know a billion things about how it works in order to use it, if what the Car is supposed to do is SIMPLER than how it does it (which is virtually always the case). Commented Jan 26, 2017 at 17:38

In an object oriented language, you have objects. Often many objects. And these objects have state, each one of them has its own state, different from any other object's state.

Problem: How would you store the state for each object? You would use instance variables, of course.

If you don't have any objects with state, for example if all your strings don't actually contain any text, all your 3D-coordinates don't have coordinates and so on, then you don't need instance variables.


Take an example of the way tying shoe laces would be implemented in OO:

There is a PairOfShoelaces class. It is responsible for tying itself, so it needs a reference to a PairOfHands class that it can use to help it do this.

Now you could have a function in PairOfShoeLaces called tieABowInYourself and it could take an instance of PairOfHands as a parameter:

function tieABowInYourself(PairOfHands hands) ...

But that means the thing calling tieABowInYourself needs to have a pair of hands laying around that it can supply as a parameter. Instead, an instance variable can be used to save away those hands inside an instance of PairOfShoeLaces. That way the function can be:

constructor(PairOfHands hands)
    this.hands = hands;

function tieABowInYourself()
    // use this.hands to help tie that bow

That way, only the thing that creates PairOfShoelaces need know about the hands.

And that is what instance variables are used for.


Create a class Point2D. Now create one instance for the point (x=50, y=100) and another instance for the point (x = -20, y = 70).

And now do this without instance variables. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.