How to initialize a shared variable in Java?

I am trying to write a method in which I need to create a temporary variable sum, general type T. Nevertheless, I get the error "Local variable sum may not have been initialized." How to initialize a shared variable? I cannot set it to 0 or 0.0, and I cannot find information on how to handle this. Here is the part of the code I'm working with:

public Matrix<T,A> multiply(Matrix<T,A> right) throws MatrixException { Matrix<T,A> temp = new Matrix<T,A>(arithmetics, rowSize, columnSize); T sum, product; if (rowSize != right.columnSize) throw new MatrixException("Row size of first matrix must match column size " + "of second matrix to multiply"); setup(temp,rowSize,columnSize); for (int i = 0; i < rowSize; i++){ for (int j = 0; j < right.columnSize; j++) { product = (arithmetics.multiply(matrix[i][j] , right.matrix[j][i])); sum = arithmetics.add(product, sum); temp.matrix[i][j] = sum; } } return temp; } 

I'm not sure this will help clarify, but here is my Arithmetics interface:

 public interface Arithmetics<T> { public T zero(); public T add( T a, T b ); public T subtract( T a, T b); public T multiply (T a, T b); public T parseString( String str ); public String toString( T a ); } 

And here is one of my classes, DoubleArithmetics, just to show how I implement the interface:

 public class DoubleArithmetics implements Arithmetics<Double> { protected Double value; public Double zero() { return new Double(0); } public Double add( Double a, Double b ) { return new Double(a.doubleValue()+b.doubleValue()); } public Double subtract (Double a, Double b) { return new Double(a.doubleValue()-b.doubleValue()); } public Double multiply (Double a, Double b) { return new Double(a.doubleValue()*b.doubleValue()); } public Double parseString( String str ) { return Double.parseDouble(str); } public String toString( Double a ) { return a.toString(); } } 
+4
source share
2 answers

Just use the zero method that you already have on your interface to initialize sum :

 T sum = arithmetics.zero(); 

For non-zero initialization, you can also add methods that take long and double values ​​and return T for them:

 public interface Arithmetics<T> { public T zero(); public T create(long l); public T create(double d); public T add( T a, T b ); public T subtract( T a, T b); public T multiply (T a, T b); public T parseString( String str ); public String toString( T a ); } 

And then implement them:

 public Double create(long l) { return new Double(l); } public Double create(double d) { return new Double(d); } 

And finally, use them:

 T one = arithmetics.create(1); 
+3
source

Creating generic files in Java is a bit complicated due to type erasure.

My approach is to pass two elements to my generic constructor class: (1) java.lang.reflect.Constructor, specific to type T; and (2) an Object [] array containing the default value of type T.

When you later want to instantiate and initialize the type T, you need to call Constructor.newInstance (Object []). In the code below, the MyGenericClass class is included in your common class (looks like it is called Matrix from your original post).

I got a solution from InstantationException for newInstance () and Create an instance of a generic type in Java?

 public class MyGenericClass<T> { Constructor _constructorForT; Object[] _initialValueForT; public MyGenericClass(Constructor constructorForT, Object[] initialValueForT) { _constructorForT = constructorForT; _initialValueForT = initialValueForT; } public void doSomething() { T sum = initializeT(_constructorForT, _initialValueForT); System.out.printf("d = %f\n", sum); } @SuppressWarnings("unchecked") private T initializeT(Constructor constructor, Object[] args) { T result = null; try { result = (T) constructor.newInstance(args); } catch (java.lang.InstantiationException ex) { } catch (java.lang.IllegalAccessException ex) { } catch (java.lang.reflect.InvocationTargetException ex) { } return result; } public static void main(String argv[]) throws Exception { Constructor constructor = Double.class.getConstructor(new Class[]{double.class}); Object[] initialValue = new Object[] { new Double(42.0) }; MyGenericClass<Double> myGenericClass = new MyGenericClass<Double>(constructor, initialValue); myGenericClass.doSomething(); } } 
0
source

Source: https://habr.com/ru/post/1488982/


All Articles