Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the exact definition of instance variable?

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?

like image 281
TalkingCode Avatar asked Nov 27 '22 02:11

TalkingCode


2 Answers

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.

like image 124
vartec Avatar answered Dec 10 '22 08:12

vartec


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.

like image 30
Naveen Avatar answered Dec 10 '22 06:12

Naveen