Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

variable might not have been initialized error

//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);
Source by www.baeldung.com #
 
PREVIOUS NEXT
Tagged: #variable #initialized #error
ADD COMMENT
Topic
Name
3+4 =