Scope of Variables In Java

This session discusses Java scope, which is intended to meet the needs of learners.

Java Scope

It is important to note that variables in Java are only accessible within the region they were created in. We refer to this as scope.



Method Scope

Any variable declared directly inside a method is available anywhere following the line in which it was declared:

Method Scope Example: 

public class Main { public static void main(String[] args) {// We cannot print int mrx here as it is declared laterint mrx = 100;// As the variable is declared now, we can easily use it down below System.out.println("Variable value: "+mrx); }// Output = Variable value: 100 }

Java Scope method examples

Example: 

public class Main { public static void main(String[] args) {// String ample cannot be printed here since it is declared laterString ample = "Java Founder: James Gosling";// Here, we can easily use the variable (String ample) because it is now declared System.out.println("Text = "+ample); }// Output :// Text = Java Founder: James Gosling }

Block Scope

All code between curly braces is considered a block of code. Variables declared inside blocks of code can be accessed only by the code between curly braces { }, following the line where the variable was declared:

Block Scope Example: 

public class Main { public static void main(String[] args) { // Variable name cannot be used here{ // The following is a block which starts from here// Variable name cannot be used hereString mrx ="Elon Musk";// Variable name can be used here System.out.println("Name: "+mrx);} // Here is the end of the block// It is not possible to use the variable name here} // Output = Name: Elon Musk }

java scope block examples

Example: 

public class Main { public static void main(String[] args) { // From here, we begin a new block{ // Here, the name of the variable cannot be used// This is where you can use a variable namechar alpha ='A';// This is where you can use a variable name System.out.println("Alphabet: "+alpha);} // A block of code comes to an end here// Using the variable name is not possible here} // Output : Alphabet: A }

It is possible for a block of code to exist alone or as part of an if, while, or for statement. If you use for statements, variables declared inside the statement are also accessible inside the block.


Java Scope Key Points:

  1. A scope is generally defined by a set of curly brackets [ ].
  2. When we are writing Java code, variables are usually accessible if they are defined within the curly brackets or inside of the curly brackets where they are defined.
  3. A class variable can be used by all member methods even if it is not defined within a method.
  4. A class variable can be referenced by the “this” keyword when a method uses the same local variable as a member.
  5. To read a variable after a loop ends, it must be set before the loop body.

 

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *