java beginner: initializing class variables

I've just read the SUN java code conventions; very nice document btw. And I've read this

6.3 Инициализация: Попробуйте инициализировать локальные переменные там, где они объявлены. Единственная причина не инициализировать variable where it’s declared is if the initial value depends on some computation occurring first.

And I was wondering if Class variables are having same suggestion or not, for example I have:

public class NNmatrix {

    protected ArrayList<ArrayList<Double>> matrix;     // line 1
    public NNmatrix() {
        matrix = new ArrayList<ArrayList<Double>>();     // line 2
    }
    /**
     * 
     * @param otherMtrx
     */
    public NNmatrix(final ArrayList<ArrayList<Double>> otherMtrx) {
        final int rows = otherMtrx.size();
        matrix = new ArrayList<ArrayList<Double>>(rows);  // line3
        for (int i = 0; i < rows; i++) {
            this.matrix.add(new ArrayList<Double>(otherMtrx.get(i)));
        }
    }
}

EDITING CODE# If I would initialize the variable where it's declared (in class), I would remove "line 2" and leave "line 3" because performance issue# reserving (rows) in memory as you know.

The question is:

  1. Is doing that a good practice or initialization matter only apply for local variables inside methods etc only?
  2. If it's fine, I want to know which will come first if I did the EDITING CODE# the initialization @ line 3 or initialization @ line 1 ?
5
задан Nadim Baraky 22 November 2016 в 16:53
поделиться