Variabel mungkin belum diinisialisasi kesalahan

//the error variant
public class VariableMightNotHaveBeenInitializedError {
    public static void main(String[] args) {
        int sum;  //variable "sum" is not initialized
        int[] list = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
        for (int i = 0; i < list.length; i++) {
            sum += list[i];
        }
        System.out.println("sum is: " + sum);
    }
}
// the fixed variant:
public class VariableMightNotHaveBeenInitializedError {
    public static void main(String[] args) {
        int sum = 0;  //variable "sum" is initialized to 0
        int[] list = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
        for (int i = 0; i < list.length; i++) {
            sum += list[i];
        }
        System.out.println("sum is: " + sum);
David Zlatin