Scope of Variables in JavaIn 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 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:
Syntax: 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. Sum of x+y = 30 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: The value of x is: 10 Let's see another example of method-level scope. DemoClass2.java Output: The value of a is: 3 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: Age is: 24 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. Sum of x+y = 30 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. BlockScopeExample2.java Output: 0 1 2 3 4 5 6 7 8 9 10
Next TopicGroovy vs Java
|