0

Basically, what is the difference (if any) between these two pieces of code.

class A {
        List<Integer> log;
        public A() {
                this.log = new ArrayList<Integer>();
             } 
    }


class A {
       List<Integer> log = new ArrayList<Integer>(); 
    }
SpiderRico
  • 1,890
  • 8
  • 28
  • 48
  • there is no difference. – Ousmane D. May 13 '17 at 22:38
  • No difference. First one is just making the compiler see the variable before it is used in the actual line, and the second is just creating the new variable as you're using it in the same line. I prefer 2nd method of variables. – user2277872 May 13 '17 at 22:47
  • 1
    Well, _technically_ I'd argue there's a difference, but the end results are the same :) – Kevin Anderson May 13 '17 at 22:52
  • Or ... to put it another way ... the technical differences do not *manifest themselves* in this example. – Stephen C May 13 '17 at 23:12
  • @KevinAnderson Technically there is what difference? – user207421 May 13 '17 at 23:57
  • There are several ways class member variables can get their initial values: in-declaration initializer, assignment in a constructor, instance initializer. There is a specific order in which the different types of initialization happen. So, _technically_, `log` gets initialized at different points in the overall object initialization process in the two versions of Class `A`. But, again, same end result. – Kevin Anderson May 14 '17 at 00:18

0 Answers0