int sum[]= new int[length];
You haven't initialized. As of now , you just declared.
And do not for get that the length
of the array
should decide at the time of initialization.
Even if you do int sum[] = null;
you'll end up with an NullPointerException
while you do sum[z]=ran;
Can't i just keep it dynamic? the length is variable
No. Arrays lenght should be fixed while initializing it. Look into Collection's in java. More specifically A List
interface with ArrayList
implementation, which is
Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null.
By writing int[] anArray = new int[10];
you are telling that
Allocate an array with enough memory for 10 integer elements and assigns the array to the anArray variable.
Seems you are new to array's and even for java. The tutorial may help you better to understand.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html