In this tutorial you will learn about the Java Variable Scope and its application with practical example.
Java Variable Scope
What Is Variable Scope?
The scope of a variable defines the region of visibility or availability for a variable, out of which we can not reference that variable. Variable declared inside main() function can not be accessed outside the main() function. The scope of a variable is limited to the curly braces containing it, if you try to access that variable outside those curly braces then you will get a compilation error. In Java, variables can be of the following two types based on their scope.
- Local Variables
- Instance Variables
- Class/Static variables
Example:-
1 2 3 4 5 6 7 8 |
class A { String name = "Steve"; //instance variable static double salary = 25000; //static variable public static void main(String[] args) { int age = 35; //local variable } } |
Java Local Variable
Variables declared inside a method, constructors, or blocks of code are called local variables. They can be accessed only inside that method or block of code. Local variables are not available outside the function it is defined in. A block begins with an opening curly brace and ends with a closing curly brace. Access specifiers cannot be used for local variables.
Example:-
1 2 3 4 5 6 7 8 9 10 11 |
public class Age { public void getAge() { int age = 25; System.out.println("My age is : " + age); } public static void main(String args[]) { Age test = new Age(); test.getAge(); } } |
Here, age is a local variable. This is defined inside the get Age() method and its scope is limited to only this method. When we run the above java program, we will see the following output.
Output:-
1 |
My age is: 25 |
Java Instance Variables
Java Instance variables are declared inside a class outside any method, constructor, or block. Instance variables can be declared at the class level and visible for all methods, constructors, and blocks in the class. Instance variables have default values. Values for instance variables can be assigned during the declaration or within the constructor. Instance variables can be accessed directly by calling the variable name inside the class, but for static methods, they should be called using the fully qualified name. Instance variables are created when an object is created and destroyed once the object is destroyed. They are also referred to as fields. Access specifiers can be given for instance variables and if nothing is mentioned the default specifier is used.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public class Employee { // this is q instance variable, and visible for any child class. public String empName; // empSalary variable is visible in Employee class only. private double empSalary; // employee name is set in the constructor. public Employee (String ename) { empName = ename; } // method to set employee salary. public void setSalary(double esalary) { empSalary = esalary; } // method prints the employee information. public void getEmpInfo() { System.out.println("Employee Name Is : " + empName ); System.out.println("Employee Salary Is : " + empSalary); } public static void main(String args[]) { Employee empOne = new Employee("Steve"); empOne.setSalary(25000); empOne.getEmpInfo(); } } |
When we run the above java program, we will see the following output.
Output:-
1 2 |
Employee Name Is : Steve Employee Salary Is : 25000.0 |
Java Class/Static Variables
In Java class variables are declared using static keywords in a class, but outside a method, constructor, or block. They are also known as static variables. Static variables are created when the program starts and destroyed when the program ends. There can be only one copy of a class variable, regardless of how many call objects are created. All of the instance of that class shares the same copy of a static variable.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Student { // pmarks is a private static variable private static double pmarks; // SUBJECT is a constant public static final String SUBJECT = "Maths "; public static void main(String args[]) { pmarks = 40; System.out.println("Passing Marks for " + SUBJECT + " is: " + pmarks); } } |
When we run the above java program, we will see the following output –
Output:-
1 |
Passing Marks for Maths is: 40); |
Note:- When class variables are declared as public static final, then variable names (constants) are all in upper case.
Static/Class variables can be accessed outside with the class name as follows –
Syntax:-
1 |
ClassName.VariableName |