Types of Variables in Java
Local Variables
Instance Variables
Static Variables
1. Local Variables
Local variables are variables that are declared within a function or a block of code and can only be accessed within that function or block. They are not known outside their respective context, meaning they have a limited scope restricted to where they are declared.
2. Instance Variables
Instance variables, also known as non-static fields, are variables declared within a class but outside any method. These variables are created when an object of the class is instantiated and are specific to each instance of the class. Each instance has its own copy of these variables. Instance variables represent the properties or state of an object.
3. Static Variables
Static variables (class variables) are variables declared with the static keyword within a class but outside any method or constructor. These variables are not tied to any specific instance of the class but belong to the class itself. As a result, they are shared among all instances of the class. A static variable is created when the class is loaded and destroyed when the program stops or the class is unloaded.
Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
There would only be one copy of each class variable per class, regardless of how many objects are created from it.
Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final, and static. Constant variables never change from their initial value.
Static variables are stored in the static memory. It is rare to use static variables other than declared final and used as either public or private constants.
Static variables are created when the program starts and destroyed when the program stops.
Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.
Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally, values can be assigned in special static initializer blocks.
Static variables can be accessed by calling with the class name ClassName.VariableName.
When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.
import java.util.*;
class TypesOfVariables
{
int s;//Instance Variable
static int x; //Class Variable
void square() //function prototype
{
int n;//Local Variable
Scanner sc=new Scanner(System.in);
System.out.print("Enter Any Number: ");
n=sc.nextInt();
s=n*n;
System.out.println("The Square of "+n+" is :"+s);
}
public static void main()
{
int n; //Local Variable to main() function
TypesOfVariables ob=new TypesOfVariables();
ob.square();
System.out.println("The Square is "+ob.s);
}
}
Example of Static variable
public class Counter
{
// Static variable
// This variable is shared among all instances of the Counter class.
static int count = 0;
// Constructor to increment the static variable 'count'
public Counter()
{
count++;
}
// Method to display the value of the static variable 'count'
public static void displayCount()
{
System.out.println("Number of instances created: " + count);
}
public static void main( )
{
// Creating instances of the Counter class
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
// All instances share the same static variable 'count'
Counter.displayCount();
// Output will be: Number of instances created: 3
}
}