In this guide, we’ll explore the different types of variables in Java, their scopes, visibility, and practical examples to help you master the concept.
We’ll also see what is meant by declaring a variable and what is meant by initialization of a variable in Java.
Declaring and Initializing a variable in Java
In Java, every variable must be declared before use. Declaration specifies the type of the variable, which can be either a primitive data type (such as int, double, char), or having class or interface as type (reference variable).
Examples of variable declaration
int age, number; // two int variables declared double amount; // a double variable declared Person person; // reference type variable declared
As you see here, in the first statement two variables of type int are declared. Note that you can declare two or more variables of same type as a comma separated list.
In the third statement a variable person is declared which is of type Person. Here Person is a class.
Java 10 introduced a new feature called local variable type inference where the type of the variable is inferred from the variable initializer. A new reserved type name “var” is added in Java to define and initialize local variables, read more about var type here- Var type in Java - Local Variable Type Inference
Initialization of a variable in Java
Initialization means providing initial value of the variable. Generally, both declaration and initialization are done in a single statement.
int age = 30; char grade = 'A';
But that is not necessary, you can also declare a variable first and initialize it later.
int age; ...... ...... age = 50;
Variables can also be initialized using expressions:
double amount; amount = 67/9;
Here amount will have the value of 67 divided by 9.
Types of variables in Java
The Java programming language defines the following kinds of variables:
-
Instance Variables (Non-Static Fields)–Instance variables are declared inside a class but outside any method, constructor,
or block, and they are not marked as static. Each object of the class gets its own copy of these variables, meaning their values
are unique to each instance.
For example, if you have a class Person and two objects of it person1 and person2 then the instance variables of these two objects will have independent values.
public class Person { private String firstName; private String lastName; private int age; private char gender; public Person(String firstName, String lastName, int age, char gender){ this.firstName = firstName; this.lastName = lastName; this.age = age; this.gender = gender; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getAge() { return age; } public char getGender() { return gender; } } public class InstanceDemo { public static void main(String[] args) { Person person1 = new Person("Ram", "Mishra", 23, 'M'); Person person2 = new Person("Amita", "Chopra", 21, 'F'); System.out.println("Values in object person1 - " + person1.getAge() + " " + person1.getFirstName() + " " + person1.getLastName()+ " " + person1.getGender()); System.out.println("Values in object person2 - " + person2.getAge() + " " + person2.getFirstName() + " " + person2.getLastName()+ " " + person2.getGender()); } }Output
Values in object person1 - 23 Ram Mishra M Values in object person2 - 21 Amita Chopra F
Here you can see how using the constructor of the class, variables are initialized for both the objects and output shows that each instance of the class has its own values for the fields.
-
Class Variables (Static Fields)- A class variable in Java is any field declared with the static modifier.
As the name suggests class variable is at the class level. Unlike instance variables, there is only one copy of a static variable
per class, shared across all objects. Doesn’t matter how many instances (objects) of the class you have, the class variable
will have the same value. You can access class variables directly using the class name, without creating an object.
Java class variables example
One common use of static fields is to create a constant value that's at a class level and applicable to all created objects.
public class Employee { int empId; String name; String dept; // static constant static final String COMPANY_NAME = "XYZ"; Employee(int empId, String name, String dept){ this.empId = empId; this.name = name; this.dept = dept; } public void displayData(){ System.out.println("EmpId = " + empId + " name= " + name + " dept = " + dept + " company = " + COMPANY_NAME); } public static void main(String args[]){ Employee emp1 = new Employee(1, "Ram", "IT"); Employee emp2 = new Employee(2, "Krishna", "IT"); emp1.displayData(); emp2.displayData(); } }Output
EmpId = 1 name= Ram dept = IT company = XYZ EmpId = 2 name= Krishna dept = IT company = XYZ
-
Local Variables– Local variables are variables declared within a method, constructor, or block. They represent the temporary state of a method and exist only during the execution of that method. Once the method finishes, the local variables are destroyed, and their values are no longer accessible.
Scope of Local Variables
The scope of a local variable is limited to the block of code enclosed by curly braces {} where it is declared. This means:- A local variable declared inside a method is accessible only within that method.
- If you declare a variable inside a nested block (such as an if statement or loop), its scope is restricted to that block.
One more thing to note is that you can have a local variable with the same name as class level variable in the method, with in the method the local variable will take priority.
Java local variables example
public class InstanceDemo { // class level variable int x = 8; public static void main(String[] args) { InstanceDemo id = new InstanceDemo(); id.display(); System.out.println("value of class level variable x " + id.x); } public void display(){ int x = 5; // local variable boolean flag = true; System.out.println("value of local variable x " + x); if (flag){ int y = 10; // nested scope variable System.out.println("value of local variable y inside if " + y); } // This will cause compile-time error //System.out.println("value of local variable y inside if " + y); } }Output
value of local variable x 5 value of local variable y inside if 10 value of class level variable x 8
Here you see there is a class level variable and again it the method display() there is a variable with the same name x. With in the method value of local variable x takes priority and that is printed. Once out of the method, x that is recognized is the class level variable x.
Another thing to note is the nested scope created by the if condition with in the display() method. Scope of variable y is in between the starting and closing braces of the if condition. Once you are out of if condition y won’t be recognized. Any attempt to print value of y outside the if condition scope will result in compile-time error.
-
Parameters- Variables passed to any method are known as parameters. Any changes made to the primitive types parameter
won’t change the original value.
Java parameters example
public class InstanceDemo { public static void main(String[] args) { InstanceDemo id = new InstanceDemo(); int x = 10; id.display(x); System.out.println("value of x after method call " + x); } public void display(int x){ x++; System.out.println("value of local variable x " + x); } }Output
value of local variable x 11 value of x after method call 10
Here you have a variable x that is passed to display method as an int parameter. With in the method display() value of x is changed. But that change is local only and doesn’t change the original value of x. This is because copy of a variable is passed as a method parameter.
If an object is passed as a parameter and any of that object’s field is changed, that change will be visible in other scopes too.
- Refer Java pass by value or pass by reference to know more about this topic.
That's all for this topic Java Variable Types With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Basics Tutorial Page
Related Topics
You may also like-





