Day 15 : Local variables and Global variables

Day 15 : Local variables and Global variables

Publish Date: Apr 6
4 2

Variable
A Variable will be Remembered only in between the opening and closing braces where it is declared.

Local Variable
It is created when a block is entered into the storage, and then it calls and destroys the block just after exiting from the function.(TBH)
Local variables are defined within a specific block of code, such as a method or a loop, and are only accessible within that block.

Example of Local Variable

class Mithra
{
 public static void main(String[] args)
 {
  int var = 89; // Declared a Local Variable

  // This variable is local to this main method only

  System.out.println("Local Variable: " + var);
 }
}
Enter fullscreen mode Exit fullscreen mode

Output

sangamithra@ideapad:~/Documents/mar20$ java Mithra 
Local Variable: 89

Enter fullscreen mode Exit fullscreen mode

Global varible

Global variables are declared at the class level, making them accessible (TBH) throughout the entire class.

Comments 2 total

Add comment