While writing the following code I get problem at lines in It is in netbeans . Whether the array declaration is accepted . If accepted then why excepton of
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code generic array creation
at mygenerics.Addition.<init>(MyGenerics.java:26)
at mygenerics.MyGenerics.main(MyGenerics.java:16)
Java Result: 1
Program here
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mygenerics;
/**
*
* @author SUMIT
*/
public class MyGenerics {
public static void main(String[] args)
{
Addition<Integer> ints = new Addition<>(1,2,3);
ints.sum();
Addition<Double> dbls = new Addition<>(1.25,2.68,3.49);
dbls.sum();
System.out.println(ints.toString());
}
}
class Addition<T extends Number>
{
**T arr[]=new T[10];**
public Addition(T... values)
{
for(int j=0;j<values.length;j++)
{
int i=0;
arr[i]=values[j];
i++;
}
System.out.println(arr);
}
public <T extends Number> void sum()
{
T sum;
System.out.print(arr);
for(int i = 0;i<arr.length;i++)
{
**sum = sum + this.arr[i];**
}
}
}
I got Generics Array creation error