Question

I think instance variables are simple data types like int or double. Everything that is created automatically when the object is created.

If an object creates additional objects - like everything that is it done with the NEW keyword - these are not instance variables.

Am I right or wrong? What is the exact definition?

Was it helpful?

Solution

Wrong. Anything that is bound within the instance (i.e. an instantiated object) is instance variable. As opposite of static (class) variables, which are bound to the class. It doesn't matter if they are simple types or pointers to objects.

OTHER TIPS

Instance variable are the ones which can be associated with an instance of a class. For example if you have

class A
{
private:
int m_a;
double m_b;
int* m_c;
};

and if you create an object (i.e. an instance) of A, one instance of m_a, m_b, m_c is created and associated with it. So these become instance variables. At the same time if you have a static variable inside the class, the static variable instance is not associated with each object of the class hence it is not an instance variable. NEW or creating a stack object are just ways of creating the objects and as such have nothing to do with instance variables.

From Wikipedia (you asked for an exact definition):

In object-oriented programming with classes, an instance variable is a variable defined in a class, for which each object in the class has a separate copy.

An instance variable is the opposite of class variable, and it is a special type of instance member.

I am new to OOP concepts, but I will try my best.

Yes, Instance Variables are variables with normal datatypes BUT they BELONG to a specific instance of OBJECT. An Instance Variable is a variable that describes "Characteristic" or "Property" of an object. e.g. carColor, carName could be a Instance Variable of class "Car" since it describes a Characteristic of object car.

When a new object is instantiated with the keyword "new" all the instance variables are automatically attached to the object and can be tracked seprately. e.g.

var carA = new car carA.carName = "Honda" carA.carColor= "Blue"

var carB = new car carA.carName = "Austin" carA.carColor= "Red"

Instance variables (aka. fields) are variables that belong to an instance, as opposed to static variables that belong to a class and local variables that belong to the local stack frame.

Your definition defines an object which is an instance of a type.

class A {
    int a;
    Foo *f;
    static int b;
};

a is an instance variable. b is not. Pointer f is an instance variable itself, the object pointed by f (created with new) is not an instance variable, because it is not even a variable, even though it is still a part of the instance state.

That depends on when and where the object creates them. If they are declared at class level, but only created after instantiation, they are still instance variables. If they are both declared and created inside a function, they are local variables, and not instance variables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top