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. Show
Scope of a VariableIn 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.
Demo.java In Java, there are three types of variables based on their 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 ModifierPackageSubclassWordpublicYesYesYesprotectedYesYesNoprivateNoNoNodefaultYesNoNoSyntax: Let's see an example. VariableScopeExample1.java Output: 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 MethodOutput: 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 ConstructorVariableInsideConstructor.java Output: Declaring a Variable Inside a BlockVariableInsideBlock.java Output: 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: 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
Intermediate PythonLevel 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 SheetWelcome 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 TutorialLearn 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 PythonExplore data analysis with Python. Pandas DataFrames make manipulating your data easy, from selecting or replacing columns and indices to reshaping your data.See MoreSee MoreIs 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!
|