Local Variable in Java

local variable can be declare  inside method or block or constructor . local variables can accessible with in the method or block or constructor in which they are declared.

  • local variables are stored in stack memory
  • final is the only  modifier applicable to local variables
  • when declaring local variables we need to initialize the values otherwise compile time error occurs.
package com.practice;

public class LocalVariableDemo {
 //method declaration
 public void add(){
  int x = 100;//local variable declaration
  int y = 200;//local variable declaration
  System.out.println("Sum of numbers :"+(x+y));

 public static void main(String[] args) {
  LocalVariableDemo localVariableDemo = new LocalVariableDemo();


Sum of numbers :300