views:

1552

answers:

7

What is the equivalent of Java's final in C#?

EDIT: Sorry, I should have been clearer. I meant what is the equivalent when applied to a member variable - so it must be assigned once and only once.

+42  A: 

The final keyword has several usages in Java. It corresponds to both the sealed and readonly keywords in C#, depending on the context in which it is used.

Classes

To prevent subclassing (inheritance from the defined class):

Java

public final class MyFinalClass {...}

C#

public sealed class MyFinalClass {...}

Methods

Prevent overriding of a virtual method.

Java

public class MyClass
{
    public final void myFinalMethod() {...}
}

C#

public class MyClass
{
    public sealed void MyFinalMethod() {...}
}

As Joachim Sauer points out, a notable difference between the two languages here is that Java by default marks all non-static methods as virtual, whereas C# marks them as sealed. Hence, you only need to use the sealed keyword in C# if you want to stop further overriding of a method that has been explicitly marked virtual in the base class.

Variables

To only allow a variable to be assigned once:

Java

public final double pi = 3.14; // essentially a constant

C#

public readonly double pi = 3.14; // essentially a constant

As a side note, the effect of the readonly keyword differs from that of the const keyword in that the expression is evaluated at runtime rather than compile-time, hence allowing arbitrary expressions.

Noldorin
Good and thorough answer!
Hemant
I'd add that all non-static methods in Java are virtual by default. So while in C# you can simply leave out the virtual in the initial definition, you'll need to use "final" to avoid subclasses overriding it in Java
Joachim Sauer
@Joachim: Good addition. I'll edit my post to include that.
Noldorin
(+1) This is THE ANSWER!!!
TheMachineCharmer
good answer - there is one more usage of "final" in java though - on a local variable or method parameter to prevent reassigning it. There is no c# direct equivalent of this.
serg10
+7  A: 

It depends on the context.

LukeH
+1 Explaining the difference would be nice though...
Hemant
@Hemant: Was editing when you posted your comment.
LukeH
@Luke: That's not quite true regarding `final` variales. Java has the same situation as C#: you can only assign it where it is declared or in the constructor.
Noldorin
@Noldorin: You're right. My Java is *very* rusty. I've edited to remove the misinformation.
LukeH
+1  A: 

sealed

x2
That only part of the answer since it depends on the context and adding an explanation and/or examples will make it a lot more digestable for those in need of help
Rune FS
+1  A: 

http://en.csharp-online.net/CSharp_FAQ:_What_are_the_differences_between_CSharp_and_Java_constant_declarations

C# constants are declared using the const keyword for compile time constants or the readonly keyword for runtime constants. The semantics of constants is the same in both the C# and Java languages.

krzyk
A: 

http://en.wikipedia.org/wiki/Comparison_of_Java_and_C_Sharp

SUMMARY:This is a comparison of the C# programming language with the Java programming language. As the two are both garbage-collected runtime-compiled languages with syntax derived from C and C++, there are many similarities between Java and C#.

adatapost
A: 

Java class final and method final -> sealed. Java member variable final -> readonly for runtime constant, const for compile time constant.

No equivalent for Local Variable final and method argument final

Vijayakumarpl
A: 

What everyone here is missing is Java's guarantee of definite assignment for final member variables.

For a class C with final member variable V, every possible execution path through every constructor of C must assign V exactly once - failing to assign V or assigning V two or more times will result in an error.

C#'s readonly keyword has no such guarantee - the compiler is more than happy to leave readonly members unassigned or allow you to assign them multiple times within a constructor.

So, final and readonly (at least with respect to member variables) are definitely not equivalent - final is much more strict.

Some guy