Made by DeepSource

Local variable is never written to JAVA-E1064

Bug risk

A local variable of a method or constructor has been found to have no writes to it.

This will cause a compile error when the project is built.

Java always expects a local variable to be assigned a value before it is read from. If no assignment is performed, Java will raise a compile error indicating that the variable was expected to be initialized before use.

Bad Practice

In the example below, x is not written to before use.

int someMethod() {
    int x;

    // x is never written to.

    return x + 5; // will cause a compiler error.


Always initialize variables to a sensible default value before using them.

int someMethod() {
    int x = 0;

    return x + 5;