Java Variable Scope

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:-

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:-

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:-

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:-

When we run the above java program, we will see the following output.

Output:-

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:-

When we run the above java program, we will see the following output –

Output:-

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:-

In this tutorial we have learn about the Java Variable Scope and its application with practical example. I hope you will like this tutorial.