Scope of Variables in Java

In Java, the scope of a variable refers to the portion of the code where the variable is accessible and can be used. There are several types of variable scopes in Java:

  1. Local Variables
  2. Instance Variables
  3. Class/ Static VariablesTypes of Variables in Java

Local Variables

A local variable is assigned a value locally. It is a variable that gets declared inside a method and can be accessed inside the same method only. For example,

Let’s see the complete example,

Instance Variables

A variable which is declared in a class, but outside the method, is known as instance variable. It is not declared as static.

For example, int id =5;

Let’s see the complete example,

Class/ Static Variables

A class variable is declared static. Class variable is also known as static variable. For example,

Let’s see the complete example,

Here’s the output for the above program, which shows the usage of Java variables:

Java Variables Output


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Data Types in Java
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading