Variable types in Java

In Java, there are 3 types of variables.

  1. Local
  2. Instance Class variables
  3. Static Class variables

Local variables are declared in methods of class. We can’t use modifiers like public/private with local variables.

Instance Class variables are declared at class level outside of all methods of class. We can use modifiers like public/private with instance variables. We need to create the objects of the class to access instance variables. Each object gets it’s own copy of instance variable in heap.

Static Class variables are declared at class level outside of all methods of class. We can use modifiers like public/private with instance variables. We can directly access static class variables using Class name. We do not need object of the class to access static variable. All objects share the same static variable.

You may also like...