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();
    }
}
Console output

Duonos kaina: 2.49 Eur

  1. The method rodytiKaina() starts executing its code.
  2. Inside it, a local double variable kaina.
  3. The method uses the value and outputs it to the console.
  4. When the method finishes, this local variable is no longer needed.

Key properties

Properties of a Java local variable
PropertyWhat does it mean?
LocationDeclared inside a method, constructor or block
ScopeAccessible only within the block where it was declared
LifetimeNeeded only while the relevant method or block executes
Initial valueJava 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
    }
}
Compiler 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
    }
}
Console output

Pasiimk: skėtis

  1. Because lyja is true, the program enters the if block.
  2. The block creates the variable daiktas and outputs its value.
  3. After the block ends, the name daiktas is 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
    }
}
Console output

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
    }
}
Compiler error

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);
    }
}
Console output

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();
    }
}
Console output

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.