chevron_left
358 points
19 13 11

In object-oriented programming languages, there are three types of variables:

- Local Variables
- Static Variables
- Instance Variables

The local variables are the ones that are declared inside a function and cannot be accessed outside of the scope of that function. The scope of a local variable starts from where it is defined inside a function and ends where the scope of the function ends. This type of variable is not specific to object-oriented programming. The other two are specifically related to object-oriented concepts. So, in this article, we are going to what static and instance variables are and what is the difference between them.


Static Variables

A static variable is an instance variable that is associated with the class rather than the object of the class. These variables do not need to be called with the instance of the class. They are declared using the "static" keyword. The syntax for declaring this type of variable is as follows:

static data_type variable_name = value;

Instance Variable

An instance variable is a member variable that is declared inside a class and cannot be accessed without creating an instance. The instance variables do not have any existence unless an object of the class is not declared. The syntax for declaring an instance variable is as follows:

access_modifier data_type variable_name;

The access _modifier in the above syntax refers to limiting the access to the variable. There are many access_modifiers but the most commonly used are public, private, and protected. If it is private, the instance variable cannot be accessed outside of the class. If it is public, it can be accessed from anywhere throughout the program. If it is protected, it can be accessed from a class itself and its child class.


Difference between Static Variable and Instance Variable

  • A static variable can be accessed with the class name whereas the instance variables are accessed with an object name.

  • A static variable can be accessed from the static or the non-static method whereas an instance variable cannot be accessed from the static method.

  • When we use a static variable, less memory is used by the program, whereas the instance variables use more memory.

  • Instance variables are created separately for each object whereas a static variable is shared among all the instances of a class.

  • Static variables act like global variables and are available to all the methods, and instance variables act like local variables.

1 Comment

You have also explained very well how to introduce a static variable.

Thanks.

More Posts

Can you change a static variable in java MallikaShaik672 - Nov 1, 2020
Cannot make a static reference to the non-static method java SharadMagar450 - Aug 5
Difference between static method and instance method amna - Mar 16
Difference between class variable and instance variable in java offonime - May 23
Difference between static and final variable in java Huzaifa-Glitch - May 29
How to call an object from another class in java? Hafsa_Rao - Aug 18
Abstraction vs encapsulation in OOP with example Hafsa_Rao - Aug 7
Difference between class method and instance method mhfaisal - Jul 1
Difference between static function and normal function in C. Tania - Sep 13
Difference between class method and static method in python Tania - Aug 9