Local Variable in Java

Let’s learn more about local variables in Java. Variables that are declared inside Methods in a Java program are called local variables.

Rule for Local Variable

  • Local variables cannot use any of the access level since their scope is only inside the method.
  • Final is the Only Non Access Modifier that can be applied to a local variable.
  • Local variables are not assigned a default value, hence they need to be initialized.

Instance Variables are defined at class level.

Example of Local Variable

package com.jbt;

/*
 * Here we will discuss about different type of Variables available in Java
 */
public class VariablesInJava {

  /*
   * Below variable is STATIC variable as it is outside any method and it is
   * using STATIC modifier with it. It is using default access modifier. To
   * know more about ACCESS MODIFIER visit appropriate section
   */
  static String staticField;
  /*
   * Below variable is INSTANCE VARIABLE as it is outside any method and it is
   * not using STATIC modifier with it. It is using default access modifier.
   * To know more about ACCESS MODIFIER visit appropriate section
   */
  int instanceField;

  public static void main(String args[]) {
    VariablesInJava obj = new VariablesInJava();

    /*
     * Instance variable can only be accessed by Object of the class only as below.
     */
    System.out.println(obj.instanceField);

    /*
     * Static field can be accessed in two way.
     * 1- Via Object of the class
     * 2- Via CLASS name
     */
    System.out.println(obj.staticField);
    System.out.println(VariablesInJava.staticField);
  }

  public void method() {
    /*
     * Below variable is LOCAL VARIABLE as it is defined inside method in
     * class. Only modifier that can be applied on local variable is FINAL.
     * To know more about access and non access modifier visit appropriate
     * section.
     *
     * Note* : Local variable needs to initialize before they can be used.
     * Which is not true for Static or Instance variable.
     */
    final String localVariable = "Initial Value";
    System.out.println(localVariable);
  }
}
public class UserController {
  // Instance variable
  private String outsideVariable;

  public void setLength() {

    // Local variable
    String localVariable = "0";

    // In order to use below line local variable needs to be initialzed
    System.out.println("Value of the localVariable is-" + localVariable);
  }
}

Naming Convention

There are no specific rules for naming a local variable. All the rules of variables are applied to local variables.

Below mentioned are the rules for naming a local variable.

  • Variable names are case sensitive.
  • There is no limitation on the length of a local variable.
  • If a variable name is of one word only then all characters should be in lower case.

Bullet Point

  • Local variables can not use any of the access levels as they exist inside the Method only.
  • The Final is the only nonaccess modifier that can be applied to a local variable.
  • Local variables don’t get a default value, hence local variables need to be initiated before they can be used.

14 Comments

    • Hi Megha,

      To understand why local variables cannot have a static modifier. You need to understand what a static modifier is.

      A static modifier is used to attach a variable or method to a class. It means if you are using static modifier against any variable/method, that variable/method will not be associated with any Instance.
      In other words, You won’t need any Instance to access the Static variable/method as it is now associated with the class itself.

      On the contrary local variables live only inside the method. Once the execution of a method completes, local variables don’t exist. So there is no point in saying that a local variable can be associated with a class.

      As you can see, both are entirely different concepts. It doesn’t make any sense to use STATIC modifier against local variables.
      And that is the reason it is not allowed.

      I hope now your doubt is evident.

      Regards

    • Hi Akhil,

      You can definitely do that. Here “0” will be treated as String and not integer. And all operation will work like it is an String and not Integer. If you want it to treat like Integer you have to convert it in Integer first.

      Regards

  1. Can we call local variables are constants variables in a method (in java) ? both constants and local variables can’t changed……..dvrtechnopark.com

    • Local variables are specific to Method only. Their lifecycle ends once execution of method competes. Hence their is no way we can access a local variable from other method in any way.

      Thannks

  2. Hello Mr. Gautam,

    At first, I would like to appreciate for the good work with your website. Well done.

    In the second program with class UserController String initialization is given 0. Isn’t that necessary to be given in double codes “0” ?

    String localVariable = 0;
    String localVariable = “0”;

    Murari Varma

    • Hi Murari,

      Thanks for your comment. I agree with you, it should be “0”.
      Article has been updated as per your suggestion.
      Do let me know if you find more typo error.

      Regards

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.