Instance /Field Variables

circle-info

We can access the instance variables with dot ( . )

triangle-exclamation

In object-oriented programmingarrow-up-right with classesarrow-up-right, an instance variable is a variablearrow-up-right defined in a class (i.e. a member variablearrow-up-right), for which each instantiated objectarrow-up-right of the class has a separate copy, or instance. An instance variable is similar to a class variablearrow-up-right.[1]arrow-up-right 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?