Mawia
Mawia

Reputation: 4310

To assign NULL to field or not

Any difference on these lines?

1.    @Autowired
      private MyClass obj;

2.    @Autowired
      private  MyClass obj = null;

3.    @Autowired
      private  MyClass obj = new MyClass();

Obviously, the third one is a bad practice.

Please explain.

Upvotes: 1

Views: 56

Answers (1)

Bohemian
Bohemian

Reputation: 424983

No difference: The instance is injected by the container regardless what what you assign to it.

Option 1 is the best and most common style used.

Upvotes: 4

Related Questions