Instance /Field Variables

We can access the instance variables with dot ( . )

In object-oriented programming with classes, an instance variable is a variable defined in a class (i.e. a member variable), for which each instantiated object of the class has a separate copy, or instance. An instance variable is similar to a class variable.[1] An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks. Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class.

Each instance variable lives in memory for the life of the object it is owned by. Source: Wikipedia

We can visualise the above code like below -

Last updated

Was this helpful?