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 its 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.

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

	/*
	 * 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;

	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 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 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.
  • Final is the only non access modifier that can be applied to local variable.
  • Local variables don’t get a default value, hence local variables need to be initiated before they can be used.
Series Navigation<< Variables in JavaInstance Variable in Java >>
By | 2017-08-14T10:55:14+00:00 April 20th, 2012|Core Java|11 Comments

About the Author:

11 Comments

  1. Murari Varma November 21, 2013 at 1:06 am - Reply

    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

    • Vivekanand Gautam November 21, 2013 at 2:42 am - Reply

      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

  2. vikraal rathore September 6, 2014 at 3:48 pm - Reply

    hey Vivekanand , how can we access local variable of one method to another method?

    • Vivekanand Gautam September 7, 2014 at 6:33 am - Reply

      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

  3. java guy October 23, 2014 at 1:18 am - Reply

    local variables are only visible inside the block

  4. venkatareddy February 17, 2015 at 5:20 pm - Reply

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

  5. shiva May 18, 2015 at 4:58 am - Reply

    super work keep going.

  6. Ankit December 31, 2015 at 10:29 am - Reply

    Nice way to explain this concept, will spread the word about the blog. Thank you !

  7. akhil September 2, 2016 at 2:18 pm - Reply

    hello,
    gautam
    in 2nd example datatype used string and we given integer value ,can we give like that.

    • J Singh September 3, 2016 at 9:54 am - Reply

      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

  8. RudraS December 22, 2016 at 12:03 pm - Reply

    Super

Leave A Comment