5

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

6.3 Initialization: Try to initialize local variables where they’re declared. The only reason not to initialize a 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 ?

4 Answers 4

4

These are instance variables, not class variables. Instance variables belong to a specific object, class variables don't (sorry to nitpick).

I think initializing the variable where it's declared is simpler and easier to read.

The jvm starts initializing instance variables and instance initializer blocks at the top of the file and works its way down, only after it has initialized all the variables and run the initializer blocks does it execute the constructor.

Sign up to request clarification or add additional context in comments.

Comments

1

The line you quoted refers only to local variables. Because in Java local variables don't need to be declared at the top of the scope but can be declared anywhere before they are used, it makes a lot of sense.

For class and instance variables, my personal preference is to initialize the variable where it is declared. In many cases where I don't have any other constructors than the default this removes the need to have a default constructor written as the compiler will automatically provide one. I find this cleans up and shortens my code.

In the second case constructor you provided you can make a good case for initializing in the constructor.

With class variables I have found few times where I would want to initialize in the instantiation block instead of inline at the declaration.

Comments

1

I would argue for leaving your code the way it is.

While in general I do like to initialize instance variables at the declaration, I do not like to do so when I must re-initialize them in some constructor. I prefer to either initialize at the declaration, or in every constructor-path (perhaps just in a single constructor called by every other constructor). The pure declaration signals that something more complicated is going on, while moving the simplest initialization to the declaration can hide that.

Comments

0

You have a couple different options that probably all fall under micro-optimizations. Typically these types of optimizations are of little concern unless you are working with static variables in a concurrent environment or doing something funky like pooling classes.

  1. There's no reason why you cant initialize your variables in your constructor or other methods similar to what is outlined here. Personally, I prefer to initialize my variables at their declaration whenever possible.

  2. From my understanding, initialization will occur at roughly the same time if occurring in the constructor vs at declaration. I've never had to know which actually comes first in practice, but you could do a simple System.out test to find out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.