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?

Scope of 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. 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 compilation error. In Java, variables can be of following two types based on their scope.

  • Local Variables
  • Instance Variables
  • Class/Static variables

Example:-

Java Local Variable

Variable declared inside a methods, constructors, or block 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 getAge() method and its scope is limited to only this method. When we run the above java program, we will see following output.

Recommended:-  Java Variables & Primitive Data Types

Output:-

Java Instance Variables

Java Instance variables are declared inside a class outside any method, constructor, or block. Instance variables can be declared in class level and visible for all methods, constructors and block 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.

Recommended:-  Java Do While Loop

Output:-

Java Class/Static Variables

In Java class variables are declared using static keyword in a class, but outside a method, constructor or a 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 created. All of the instance of that class shares same copy of a static variable.

Example:-

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

Output:-

Note:- When class variables is declared as public static final, then variable names (constants) are all in upper case.

Static/Class variables can be accessed outside the with the class name as following –

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.