Skip to main content

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.
Example
File:LocalVariableDemo.java
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();
      localVariableDemo.add();
  

 }

}
output
Sum of numbers :300

Popular posts from this blog

Java Primitive DataTypes

Java data types will tell us what type of data can variable hold. Ex: int x = 10; In the above variable, x can hold int data type value.so we must declare any variable with data type before we can use a variable.

Instance Variables in Java

Instance variables should declare with in the class but outside of any method or block or constructor.instance variables can accessible inside class all methods , blocks & constructors.

Angular 9 Date Pipe Example

Table of Contents what is pipe in angular? Angular Date Pipe Pre-defined Date Format Custom Date Formats what is pipe in angular? In Angular Pipes are used to transform data.Pipes will take input data and transforms input data in desired output. Symbol of Pipe : " | " Angular Date Pipe Angular DatePipe is one of built-in Pipes.DatePipe is used for Format a date value according to locale rules. Syntax {{value_expression| date [:format[:timezone[:locale]]]}} DatePipe avalibale in '@angular/common' module. Example //app.component.ts import   {   Component   }   from   '@angular/core' ; @ Component ({   selector:   'app-root',    template:   `{{dOB | date:'fullDate'}}`,    styleUrls:  ['./app.component.css'] }) expo...