Variable Categories in Java — Java programming #13

Sineth Shashintha
2 min readMay 1, 2023

--

Local Variables

A local variable is a variable that is declared within a method, constructor, or block, and is only visible and accessible within that scope. This means that you cannot access a local variable outside of the method, constructor, or block in which it is declared.

Local variables are created when the method, constructor, or block is entered, and are destroyed when it is exited. This means that their values only exist for the duration of the method, constructor, or block. Local variables must be initialized before they can be used in Java.

Local variables can have any of the primitive or reference types in Java, and can be modified within the method, constructor, or block where they are declared.

Instance Variables

An instance variable, also known as a member variable, is a variable that is declared within a class, but outside of any method, constructor, or block. Instance variables belong to the instance of the class, and are therefore unique to each instance.

Instance variables can have any of the primitive or reference types in Java, and can be modified using methods defined within the class. They can be accessed using an instance of the class, and their values persist as long as the instance exists. This means that instance variables can be used to maintain state information for objects of the class.

Static/Class Variables

a static variable is a variable that belongs to a class rather than an instance of the class. Static variables are also known as class variables because their values are shared across all instances of the class.

Static variables are declared using the static keyword, and are typically used to store values that are common to all instances of a class, such as constants or configuration values. Because static variables belong to the class itself, they can be accessed using the class name, rather than an instance of the class.

--

--

Sineth Shashintha

I am a Software Engineering Undergraduate student with the passion of modern technology. Always trying to be happy and make the moments happy respecting others