Day 15 : Local variables and Global variables

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); } } Output sangamithra@ideapad:~/Documents/mar20$ java Mithra Local Variable: 89 Global varible Global variables are declared at the class level, making them accessible (TBH) throughout the entire class.

Apr 6, 2025 - 01:55
 0
Day 15 : Local variables and Global variables

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);
 }
}

Output

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

Global varible

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