JAVA VARIABLES
Java local variables
A local variable is declared inside a method, constructor or code block. It holds a temporary value needed only for a particular operation.
What is a local variable?
In this example, kaina is local to the method rodytiKaina() :
public class Main {
static void rodytiKaina() {
double kaina = 2.49;
System.out.println("Duonos kaina: " + kaina + " Eur");
}
public static void main(String[] args) {
rodytiKaina();
}
}Duonos kaina: 2.49 Eur
- The method
rodytiKaina()starts executing its code. - Inside it, a local
doublevariablekaina. - The method uses the value and outputs it to the console.
- When the method finishes, this local variable is no longer needed.
Key properties
| Property | What does it mean? |
|---|---|
| Location | Declared inside a method, constructor or block |
| Scope | Accessible only within the block where it was declared |
| Lifetime | Needed only while the relevant method or block executes |
| Initial value | Java does not provide one — the developer must initialise the variable |
A variable is visible only in its own method
The variable kaina is created in the method rodytiKaina(), so the method main() cannot access it directly.
public class Main {
static void rodytiKaina() {
double kaina = 2.49;
System.out.println(kaina);
}
public static void main(String[] args) {
rodytiKaina();
System.out.println(kaina); // Compilation error
}
}error: cannot find symbol
symbol: variable kaina
Different methods are like separate rooms: an object placed in one room is not automatically available in another.
A local variable in a code block
Braces { ... } create a block. A variable declared inside it remains in scope only up to the closing brace.
public class Main {
public static void main(String[] args) {
boolean lyja = true;
if (lyja) {
String daiktas = "skėtis";
System.out.println("Pasiimk: " + daiktas);
}
// System.out.println(daiktas); // daiktas is no longer accessible here
}
}Pasiimk: skėtis
- Because
lyjaistrue, the program enters theifblock. - The block creates the variable
daiktasand outputs its value. - After the block ends, the name
daiktasis no longer accessible in that method.
A local loop variable
for loop counter declared in the loop header is also a local variable.
public class Main {
public static void main(String[] args) {
for (int diena = 1; diena <= 3; diena++) {
System.out.println("Diena " + diena);
}
// System.out.println(diena); // The variable is unavailable outside the loop
}
}Diena 1
Diena 2
Diena 3
The variable diena is needed only for the loop. When the loop ends, this name cannot be used outside it.
Assign a value before use
Java does not automatically assign default values to local variables:
public class Main {
public static void main(String[] args) {
int obuoliuSkaicius;
System.out.println(obuoliuSkaicius);
// Error: the variable has not been assigned a value
}
}error: variable obuoliuSkaicius might not have been initialized
You can declare the variable on one line, but you must assign its value before reading it:
public class Main {
public static void main(String[] args) {
int obuoliuSkaicius;
obuoliuSkaicius = 5;
System.out.println("Obuolių: " + obuoliuSkaicius);
}
}Obuolių: 5
The same name in different methods
Two methods may have local variables with the same name because their scopes do not overlap.
public class Main {
static void ruostiPusrycius() {
String gerimas = "arbata";
System.out.println("Pusryčiams: " + gerimas);
}
static void ruostiVakariene() {
String gerimas = "vanduo";
System.out.println("Vakarienei: " + gerimas);
}
public static void main(String[] args) {
ruostiPusrycius();
ruostiVakariene();
}
}Pusryčiams: arbata
Vakarienei: vanduo
Each gerimas belongs only to its own method. Changing the value in one method would not change the variable in the other.
What should you remember?
- Declare a local variable as close as possible to where it is used.
- Always assign it a value before its first use.
- Do not expect to access the variable outside its method or block.
- Choose a clear name that describes the purpose of the temporary value.
- A local variable cannot be marked
static.