Search
 
SCRIPT & CODE EXAMPLE
 

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);
Comment

PREVIOUS NEXT
Code Example
Java :: swap two nodes of a linked list 
Java :: object in java 
Java :: how to return an object in java 
Java :: meter to cm 
Java :: try catch in java 
Java :: calculating the percentile in java 
Java :: jre in java 
Java :: prime factors of a number 
Java :: vim yank to clipboard 
Java :: how to use another class in java inside of an action listenrer 
Java :: java stream Return sums of elements grouped by a remainder of division by the give divisor 
Java :: how to call the main method in java 
Java :: kingkaihockey 
Java :: java cant use string functions after changing an integer into string 
Java :: Java program to print the character or a letter x using star 
Java :: map indexof java 
Java :: Marging ViewPager 
Java :: syntax for new module creation in java 
Java :: var keyword not working in spring boot application 
Java :: android stop audio playing by activity lifecycle 
Java :: hgjhghj 
Java :: jmonkey shapes 
Java :: what does .set do in java 
Java :: more parameters java sql @PathVariable 
Java :: android bootom app bar tab bar 
Java :: setting a hint on a text field in javafx 
Java :: byte array to zip java 
Java :: call method without creating object java 
Java :: how to add a hyperlink in a string in java mail 
Java :: nbubble sort java 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =