Possible Duplicate:
What is the equivalent of Java’s final in C#?
In Java final applies to more than just a class.
So, I wonder: is there any functional difference between the two keywords?
Thank you, and sorry for a relatively noob question.
A quick Google search did not satisfy my needs.
Java’s
finalkeyword is the equivalent of C#’ssealed,readonly, andsealedkeywords.Two of those three are somewhat different in Java and C#:
In Java, methods are virtual by default, so any method can be declared
finalto prevent it from being overriden. In C#, methods are not virtual by default, so the only overridden methods can be declaredsealed(To prevent them from being further overridden)In Java, any variable or field can be declared
finalto prevent it from being changed after initialization (And, for fields, outside the constructor). In C#, fields can be declaredreadonly, for the exact same effect, but local variables cannot. Note that Java has no equivalent of C#’sconstkeyword. (consts in C# are evaluated at compile-time, and the values are hard-coded instead of being referenced)For classes, C#’s
sealedclasses and Java’sfinalclasses are exactly the same (AFAIK).