What is the difference between instantiation in constructor or in field definition?

后端 未结 5 1697
后悔当初
后悔当初 2021-01-14 16:23

What is the difference between this:

public class Foo {
    private Bar bar;
    public Foo() { bar = new Bar(); }
}

and this:



        
5条回答
  •  无人及你
    2021-01-14 16:47

    There is no difference, because a compiler places all field definitions before constructor's code. So in IL-code both variants look equally.

提交回复
热议问题