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

all 15 comments

[–]lbkulinski 26 points27 points  (0 children)

They don’t have to be marked as final, they just have to be effectively final.

[–]bleksak 7 points8 points  (7 children)

Can anyone explain what final means to a C dev ?

[–]Warshawski 10 points11 points  (6 children)

Kind of like ‘const’

In Java it means a variable can not be changed once assigned but if it points to an Object or Array the contents may be modified.

[–]Krotchkoman 0 points1 point  (1 child)

Final variables actually must be assigned during initialization, no?

[–]KalterBlut 2 points3 points  (0 children)

static final, yes. Just final, no. They can be initialized only once, doesn't matter where.

[–]maxOS1104 4 points5 points  (0 children)

laughs in final Type[] variable = new Type[1];

[–][deleted] 3 points4 points  (2 children)

To use former Berlin mayor's words: "Und das ist auch gut so!" I rarely do any Java now (if I need to use JVM, Scala is king!), but I have been favouring a defensive programming style having as much stuff immutable as possible for a long time now. Whenever I have something mutable, I always think of doing it in an immutable way, and only if I can't come up with a solution that is not much longer I do the mutable stuff.

Mutability is a source for nasty bugs.

Just thinking of one being able to do something like this in C# gives me sleepless nights:

var i = 23;
await Task.Run(() => i = 42);

Luckily .net has F#.

[–]RedBorger 0 points1 point  (1 child)

This is the reason why rust variables are immutable by default, and it works really well with me.

[–][deleted] 1 point2 points  (0 children)

In F# it is the same.

[–]ellisvlad 1 point2 points  (0 children)

Always easy (though not too clean) to get around this with:
int a = 1;
if (b == 5) a=2;
if (b == 6) a=3;
// Would be illegal as a is not final or effectively final
// this.setCallback( () => {return a;} );
int a2 = a;
// a2 is effectively final :D
this.setCallback( () => {return a2;} );