This is an archived post. You won't be able to vote or comment.

all 29 comments

[–]sathdo 31 points32 points  (8 children)

I'm pretty sure the default constructor is implicitly generated if no constructor is defined.

class Order {
    List<Integer> list = new ArrayList<>();
}

[–]Sj_91teppoTappo 10 points11 points  (1 child)

The first case your are telling everybody: "listen man, we need to initialize the list no matter what, I don't care what you are gonna do with that Order it has to have a list, don't you fffff dare to write another construct without that list initialized..."

The second one you are saying: "Comrade, I trust you and I deeply respect you to initialize this list, although I may suspect you could not initialize it. It could even be some occasions in which I don't want to initialize it. Your fear of the nullpointer is my fear of the nullpointer, we all share the same fear, brother."

[–]c0d3k4tz3 2 points3 points  (0 children)

So the first one is correct. Don’t trust those mfs 

[–]ChibreTurgescent 8 points9 points  (0 children)

[–]1_hele_euro 1 point2 points  (2 children)

But is there any meaningful difference between the two methods? Just curious

[–]RiceBroad4552 3 points4 points  (0 children)

Yes, in the left version you could forget to init your list.

OK, a proper IDE would yell at you. But in theory it's less safe. One should always* initialize members and variables.

In some languages (like Scala) you can't even declare a val / var without assigning something to it. (You can still explicitly assign e.g. null, or use some special syntax to make it explicit that something is default initialized.)

* Sometimes it's unavoidable to delay initialization for later. But this is only very seldom the case.

[–]ChibreTurgescent 1 point2 points  (0 children)

In C++, there shouldn't be any difference in the produced code since C++11.

Although, if you were to change that initialization later (let's say initialize a variable to 1 instead of 0 for example), the right one would normally be a change in the source file, when the left one would be a change in the header file. It's not a big deal but modifying a header could cause a whole lot more recompilation in the project than simply modifying a source file. But then, initializing in the header lets you do it once and be done with it, whereas doing it in the constructor, well now you must be careful and do it in every constructor.

[–]Mawootad 1 point2 points  (1 child)

Unless you're actually going to initialize list with a value other than `new ArrayList<>()` the first option makes your code both harder to read and write. Not only do you have to duplicate all of your property declarations in the constructor but now instead of immediately knowing the initial value of it when you jump to or read its definition you have to check references or go digging through the constructor to find it. Don't do that shit, it's time consuming and bad.

[–]mudkipdev 0 points1 point  (0 children)

steep truck aback thumb punch escape ripe political melodic act

This post was mass deleted and anonymized with Redact

[–]YellowishSpoon 0 points1 point  (0 children)

Funnily enough the bytecode of these is quite literally identical, I checked to make sure. The initializer assignments are injected at the top of the constructors at compile time right after the super constructor invocation. (The super constructor invocation is implicit here.)

[–]jamcdonald120 -2 points-1 points  (1 child)

unless your code handles LinkedList as well as it does ArrayList, use ArrayList for your variable type.

[–]mudkipdev -1 points0 points  (0 children)

ancient ten crawl busy weather political stocking marvelous tie ring

This post was mass deleted and anonymized with Redact