Prev Next

Java / Local Variables

Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. Define local variable.
Posted on Apr 1, 2016 by Senthil Kumar.

Local variable also known as block level variable, are identifiers that are declared within a block of code or method.

This variable need to be initialized before using it.

In the below examples, "i" variable is an example of local variable and it is visible only to the enclosing block.

{
      int i;
}

void methodName() {
		int i;
	}

2. What is the default value of local variables or method variables?

The local or method level variables are not initialized to any default value, neither primitives nor object references.

3. What is inline value in Java?

Inline value represents the literals in a Java program.

For example, String str = "Hello", here the string "Hello" is a inline value.

4. Difference between final and effectively final in Java.

The keyword final ensures that the variable is initialized only once and cannot be altered. Reinitializing would issue compilation errors.

Any variable that is initialized only once and not marked as final is known as effectively final. This variable when marked final will not issue any compilation errors.

5. Can a local class access the variables from enclosing block in Java?

Yes. From Java 8, local variables are accessible from local class that are final or effectively final. Prior to Java 8, it allowed only final variables.

6. What is synthetic field in Java?

Synthetic fields are compiler generated fields to hold reference to the inner local class which is required by JVM.

Synthetic fields are scoped private.

«
»
Instance Variables (Non Static Variables)

Comments & Discussions