Java - Class Local Variables

What is Local variable?

A Local variable is declared inside a method, a constructor, or a block.

Scope

A local variable declared inside a method or block exists only for that block of code.

A local variable is scoped inside its definition block.

The parameters of a method are local variables.

Rule

Here are some rule regarding the usage of local variables.

  • Local variables are not initialized by default while an instance/class variable is initialized with a default value.
  • A local variable cannot be accessed in the program until it is assigned a value.
  • You do not need to declare all local variables at the start of the block.
  • A local variable shadows an instance variable and a class variable with the same name.

Quiz