Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="http://en.wikipedia.org/wiki/Final_%28Java%29" rel="noreferrer"><code>final</code></a> keyword has several usages in Java. It corresponds to both the <a href="http://msdn.microsoft.com/en-us/library/88c54tsw.aspx" rel="noreferrer"><code>sealed</code></a> and <a href="http://msdn.microsoft.com/en-us/library/acdd6hb7.aspx" rel="noreferrer"><code>readonly</code></a> keywords in C#, depending on the context in which it is used.</p> <h2>Classes</h2> <p>To prevent subclassing (inheritance from the defined class):</p> <p><strong>Java</strong> </p> <pre><code>public final class MyFinalClass {...} </code></pre> <p><strong>C#</strong></p> <pre><code>public sealed class MyFinalClass {...} </code></pre> <h2>Methods</h2> <p>Prevent overriding of a <code>virtual</code> method.</p> <p><strong>Java</strong></p> <pre><code>public class MyClass { public final void myFinalMethod() {...} } </code></pre> <p><strong>C#</strong></p> <pre><code>public class MyClass : MyBaseClass { public sealed override void MyFinalMethod() {...} } </code></pre> <p>As Joachim Sauer points out, a notable difference between the two languages here is that Java by default marks all non-static methods as <code>virtual</code>, whereas C# marks them as <code>sealed</code>. Hence, you only need to use the <code>sealed</code> keyword in C# if you want to stop further overriding of a method that has been explicitly marked <code>virtual</code> in the base class.</p> <h2>Variables</h2> <p>To only allow a variable to be assigned once:</p> <p><strong>Java</strong></p> <pre><code>public final double pi = 3.14; // essentially a constant </code></pre> <p><strong>C#</strong></p> <pre><code>public readonly double pi = 3.14; // essentially a constant </code></pre> <p>As a side note, the effect of the <code>readonly</code> keyword differs from that of the <a href="http://msdn.microsoft.com/en-us/library/e6w8fe1b.aspx" rel="noreferrer"><code>const</code></a> keyword in that the <code>readonly</code> expression is evaluated at <em>runtime</em> rather than <em>compile-time</em>, hence allowing arbitrary expressions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload