Of a variable determines the part of the program in which it is accessible

In programming, scope of variable defines how a specific variable is accessible within the program or across classes. In this section, we will discuss the scope of variables in Java.

Scope of a Variable

In programming, a variable can be declared and defined inside a class, method, or block. It defines the scope of the variable i.e. the visibility or accessibility of a variable. Variable declared inside a block or method are not visible to outside. If we try to do so, we will get a compilation error. Note that the scope of a variable can be nested.

  • We can declare variables anywhere in the program but it has limited scope.
  • A variable can be a parameter of a method or constructor.
  • A variable can be defined and declared inside the body of a method and constructor.
  • It can also be defined inside blocks and loops.
  • Variable declared inside main() function cannot be accessed outside the main() function
Of a variable determines the part of the program in which it is accessible

Demo.java

In Java, there are three types of variables based on their scope:

  1. Member Variables (Class Level Scope)
  2. Local Variables (Method Level Scope)

Member Variables (Class Level Scope)

These are the variables that are declared inside the class but outside any function have class-level scope. We can access these variables anywhere inside the class. Note that the access specifier of a member variable does not affect the scope within the class. Java allows us to access member variables outside the class with the following rules:

Access ModifierPackageSubclassWordpublicYesYesYesprotectedYesYesNoprivateNoNoNodefaultYesNoNo

Syntax:

Let's see an example.

VariableScopeExample1.java

Output:

Of a variable determines the part of the program in which it is accessible

We see that y=100 is unknown. If you want to compile and run the above program remove or comment the statement y=100. After removing the statement, the above program runs successfully and shows the following output.

There is another variable named an instance variable. These are declared inside a class but outside any method, constructor, or block. When an instance variable is declared using the keyword static is known as a static variable. Their scope is class level but visible to the method, constructor, or block that is defined inside the class.

Let's see an example.

Product.java

Output:

Product Name: Mac Book
Product Price: 65000.0

Let's see another example.

StaticVariableScope.java

Output:

The value of PI is: 3.14159265359

Local Variables (Method Level Scope)

These are the variables that are declared inside a method, constructor, or block have a method-level or block-level scope and cannot be accessed outside in which it is defined. Variables declared inside a pair of curly braces {} have block-level scope.

Declaring a Variable Inside a Method

Output:

Let's see another example of method-level scope.

DemoClass2.java

Output:

In the above example, we have passed a variable as a parameter. We have used this keyword that differentiates the class variable and local variable.

Declaring a Variable Inside a Constructor

VariableInsideConstructor.java

Output:

Declaring a Variable Inside a Block

VariableInsideBlock.java

Output:

Of a variable determines the part of the program in which it is accessible

We see that y=100 is unknown. If you want to compile and run the above program remove or comment the statement y=100. After removing the statement, the above program runs successfully and shows the following output.

Let's see another example.

BlockScopeExample1.java

Output:

Of a variable determines the part of the program in which it is accessible

When we run the above program, it shows an error at line 9, cannot find symbol because we have tried to print the variable x that is declared inside the loop. To resolve this error, we need to declare the variable x just before the for loop.

Skip to main content

Introduction to Python

Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.

Intermediate Python

Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.

Related

SQL vs Python: Which Should You Learn?

In this article, we will cover the main features of Python and SQL, their main similarities and differences, and which one you should choose first to start your data science journey.

Javier Canales Luna •

12 min

Text Data In Python Cheat Sheet

Welcome to our cheat sheet for working with text data in Python! We've compiled a list of the most useful functions and packages for cleaning, processing, and analyzing text data in Python, along with clear examples and explanations, so you'll have everything you need to know about working with text data in Python all in one place.

Python Sets and Set Theory Tutorial

Learn about Python sets: what they are, how to create them, when to use them, built-in functions and their relationship to set theory operations.

Pandas Tutorial: DataFrames in Python

Explore data analysis with Python. Pandas DataFrames make manipulating your data easy, from selecting or replacing columns and indices to reshaping your data.

See MoreSee More

Is a variable that is only accessible within a specific part of a program?

A local variable is a variable which is either a variable declared within the function or is an argument passed to a function. As you may have encountered in your programming, if we declare variables in a function then we can only use them within that function.

Which variable is one that is accessible to all parts of a program and is usually declared as part of the first lines of code?

Global Variables A global variable is one that is accessible to all parts of a program and is usually declared as part of the first lines of code.

Which variables can be accessible by that function only?

Local Variables This means that these variables are visible and can only be used by the function in which the variable is defined!