DEV Community

Sangamithra K
Sangamithra K

Posted on

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

Top comments (2)

Collapse
 
nevodavid profile image
Nevo David

Amazing explanation of variables!

Collapse
 
eloquens profile image
Eloquens AI

Nice!