Eclipse: Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The local variable average may not have been initialized
at Apples.main(Apples.java:11)
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Eclipse: Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The local variable average may not have been initialized
at Apples.main(Apples.java:11)
This is an example of Definite assignment analysis - Wikipedia, the free encyclopedia
As a general rule, when a compiler error tells you something has not been done yet (in this case 'average' has not been initialized), then its probably best to do what it suggests.
im a beginner and i didnt understand most of the link you embedded, would you mind explaining in a bit more detail please?
The compiler is asking you to give the variable an initial value.variable average may not have been initialized
If you don't understand my answer, don't ignore it, ask a question.
copeg (May 14th, 2014)
i think this is better, correct?public class Apples{ public static void main (String args[]){ int bucky[]={21,16,86,21,3}; int sum=0; int average=sum; for(int counter=0;counter<bucky.length;counter++){ sum+=bucky[counter]; average=sum/bucky.length; } System.out.println("The sum of the numbers = " + sum); System.out.print(average); } }
Why assign sum to average, why not 0?
What does the compiler think?i think this is better
If you don't understand my answer, don't ignore it, ask a question.
all class variables are initialized in class instatiation phase that mean when object is created class level variables are initialized by their default values but in case of local variable "When you declare any local/block variable, they didn’t get the default values. They must assigned some value before accessing it other wise compiler will throw an error."
and your average is local variable in ur program so you have to initialized it