Scroll to top

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.
Author avatar
JBT
https://javabeginnerstutorial.com/

12 comments

  1. Murari Varma

    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

      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

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

    • Vivekanand Gautam

      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

    local variables are only visible inside the block

  4. venkatareddy

    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

    super work keep going.

  6. Ankit

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

  7. akhil

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

    • 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. Bill

    Very good, thank you.

Post a comment

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.