What is the exact definition of an instance variable?

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

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

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

+4
source share
7 answers

Wrong. Everything that is connected inside an instance (i.e. an instance of an object) is an instance variable. Unlike static (class) variables associated with the class. It doesn't matter if they are simple types or pointers to objects.

+18
source

An instance variable is one that 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) A, one instance m_a, m_b, m_c is created and associated. Thus, they become instance variables. At the same time, if you have a static variable inside the class, the instance of the static variable is not associated with each object of the class, therefore, it is not an instance variable. NEW or creating a stack object are simply ways to create objects and as such have nothing to do with instance variables.

+6
source

From Wikipedia (you requested 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 a class variable, and it is a special type of instance member.

+5
source

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

Yes, instance variables are variables with ordinary data types, but they appear in a specific instance of an OBJECT object. An instance variable is a variable that describes the "Feature" or "Property" of an object. for example carColor, carName can be an instance of the Car class variable, because it describes the characteristic of a car object.

When a new object is created with the keyword "new", all instance variables are automatically bound to the object and can be tracked separately. eg

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

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

+2
source

Instance variables (aka. Fields) are variables that belong to the instance, unlike static variables that belong to the class and local variables that belong to the local frame of the stack.

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

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

a is an instance variable. b no. The pointer f is the instance variable itself, the object denoted by f (created with new ) is not an instance variable because it is not even a variable, although it is still part of the state of the instance.

+1
source

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

0
source

All Articles