Content deleted Content added
No bare URLs. Remove Cleanup bare URLs template |
|||
(6 intermediate revisions by 3 users not shown) | |||
Line 1:
{{Short description|Keyword in the Java programming language}}
{{lowercase title}}
In the [[Java (programming language)|Java programming language]], the <code>'''final'''</code> [[Keyword (computing)|keyword]] is used in several contexts to define an entity that can only be assigned once.
Line 9 ⟶ 10:
Example:
<syntaxhighlight lang="java">
public final class
// ...
}
// Forbidden
public class DerivedClass extends FinalClass {
// ...
}
</syntaxhighlight>
Line 20 ⟶ 26:
Example:
<syntaxhighlight lang="java">
public class Base {
public
▲ public final void m2() {...}
public static
public static final void m4() { ... }
}
public class Derived extends Base {
public void
▲ public void m2() {...} // forbidden
public static void m3() { ...
public static void m4() { ...
}
</syntaxhighlight>
Line 52 ⟶ 56:
public class Sphere {
//
public static final double PI = 3.141592653589793;
Line 61 ⟶ 65:
Sphere(double x, double y, double z, double r) {
}
Line 155 ⟶ 159:
final boolean hasTwoDigits;
if (number >= 10 && number < 100) {
hasTwoDigits = true;
}
if (number > -100 && number <= -10) {
hasTwoDigits = true; // compile-error because the final variable might already be assigned.
}
</syntaxhighlight>
Line 167 ⟶ 171:
if (number % 2 == 0) {
isEven = true;
}
Line 180 ⟶ 184:
if (number % 2 == 0) {
isEven = true;
}
Line 189 ⟶ 193:
==C/C++ analog of final variables==
{{further|const (computer programming)}}
In [[C (programming language)|C]] and [[C++]], the analogous construct is the <code>[[const (computer programming)|const]]</code> [[keyword (computer programming)|keyword]]. This differs substantially from <code>final</code> in Java, most basically in being a [[type qualifier]]: <code>const</code> is part of the ''[[data type|type]],'' not only part of the identifier (variable). This also means that the constancy of a value can be changed by casting (explicit type conversion), in this case known as "const casting". Nonetheless, casting away constness and then modifying the object results in [[undefined behavior]] if the object was originally declared <code>const</code>.
Further, because C and C++ expose pointers and references directly, there is a distinction between whether the pointer itself is constant, and whether the data pointed to by the pointer is constant. Applying <code>const</code> to a pointer itself, as in <code>SomeClass
In C++, the <code>final</code> keyword is used to denote that a function cannot be further overridden. It is also used similarly to Java to declare a class as final (cannot be extended).
<syntaxhighlight lang="C++">
// final in a class declaration declares that a class cannot be extended
class Z final : public X, public Y {
public:
// final in a method signature declares that a method cannot be overridden further
void someOperation() override final {
// do something here
}
};
</syntaxhighlight>
==C# analogs for final keyword ==
[[C Sharp (programming language)|C#]] can be considered as similar to Java, in terms of its language features and basic syntax: Java has JVM, C# has .Net Framework; Java has bytecode, C# has MSIL; Java has no pointers (real memory) support, C# is the same.
Regarding the final keyword, C# has two related keywords:
|