GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing en.wikipedia.org — final (Java) - Wikipedia In the Java programming language, the Once a Final classesA final class cannot be subclassed. As doing this can confer security and efficiency benefits, many of the Java standard library classes are final, such as Example: final class Final { // ... } // Forbidden class Derived extends Final { // ... } Final methodsA final method cannot be overridden or hidden by subclasses.[3] This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.[4] Example: public class Base { public void foo() { // ... } public final void bar() { // ... } public static void baz() { // ... } public static final void qux() { // ... } } public class Derived extends Base { // OK, overriding Base::foo() @Override public void foo() { // ... } // forbidden @Override public void bar() { // ... } // OK, overriding Base::baz() @Override public static void baz() { // ... } // forbidden @Override public static void qux() { // ... } } A common misconception is that declaring a method as Machine code compilers that generate directly executable, platform-specific machine code, are an exception. When using static linking, the compiler can safely assume that methods and variables computable at compile-time may be inlined. Final variablesA final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a "blank final" variable. A blank final instance variable of a class must be definitely assigned in every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared; otherwise, a compile-time error occurs in both cases.[6] (Note: If the variable is a reference, this means that the variable cannot be re-bound to reference another object. But the object that it references is still mutable, if it was originally mutable.) Unlike the value of a constant, the value of a final variable is not necessarily known at compile time. Typically, Example: public class Sphere { // Pi is a universal constant, about as constant as anything can be. public static final double PI = 3.141592653589793; // or Math.PI public final double radius; public final double xPos; public final double yPos; public final double zPos; Sphere(double x, double y, double z, double r) { radius = r; xPos = x; yPos = y; zPos = z; } // ... } Any attempt to reassign To illustrate that finality doesn't guarantee immutability: suppose we replace the three position variables with a single one: public final Position pos; where Like full immutability, the use of final variables has great advantages, especially in optimization. For instance, Though it appears to violate the for (final SomeObject obj : someList) { // do something with obj } Since the Final variables in nested objectsFinal variables can be used to construct trees of immutable objects. Once constructed, these objects are guaranteed not to change anymore. To achieve this, an immutable class must only have final fields, and these final fields may only have immutable types themselves. Java's primitive types are immutable, as are strings and several other classes. If the above construction is violated by having an object in the tree that is not immutable, the expectation does not hold that anything reachable via the final variable is constant. For example, the following code defines a coordinate system whose origin should always be at . The origin is implemented using a import java.awt.Point; public class FinalDemo { static class CoordinateSystem { private final Point ORIGIN = new Point(0, 0); public Point getOrigin() { return ORIGIN; } } public static void main(String[] args) { CoordinateSystem coordinateSystem = new CoordinateSystem(); coordinateSystem.getOrigin().x = 15; assert coordinateSystem.getOrigin().getX() == 0; } } The reason for this is that declaring a variable final only means that this variable will point to the same object at any time. The object that the variable points to is not influenced by that final variable though. In the above example, the origin's and coordinates can be freely modified. To prevent this undesirable situation, a common requirement is that all fields of an immutable object must be final, and that the types of these fields must be immutable themselves. This disqualifies Final and inner classesWhen an anonymous inner class is defined within the body of a method, all variables declared package org.wikipedia.examples; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class FooGUI { public static void main(String[] args) { // initialize GUI components final JFrame jf = new JFrame("Hello world!"); // allows jf to be accessed from inner class body jf.add(new JButton("Click me")); // pack and make visible on the Event-Dispatch Thread SwingUtilities.invokeLater(new Runnable() { @Override public void run() { jf.pack(); // this would be a compile-time error if jf were not final jf.setLocationRelativeTo(null); jf.setVisible(true); } }); } } Blank finalThe blank final, which was introduced in Java 1.1, is a final variable whose declaration lacks an initializer.[9][10] Previous to Java 1.1, a final variable was required to have an initializer. A blank final, by definition of final boolean hasTwoDigits; if (number >= 10 && number < 100) { hasTwoDigits = true; } if (number > -100 && number <= -10) { // compile-error because the final variable might already be assigned. hasTwoDigits = true; } In addition, a blank final also has to be definitely assigned before being accessed.[11] final boolean isEven; if (number % 2 == 0) { isEven = true; } // compile-error because the variable was not assigned in the else-case. System.out.println(isEven); Note though that a non-final local variable also needs to be definitely assigned before being accessed.[11] boolean isEven; // NOT final if (number % 2 == 0) { isEven = true; } // Same compile-error because the non-final variable was not assigned in the else-case. System.out.println(isEven); ReflectionDespite initialized final fields being intended to be immutable, they can be changed through reflection. The Analogous conceptsIn C and C++, the analogous construct is the C and C++ additionally use 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 In C++, the // 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 } }; 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 CIL; Java has no pointers (real memory) support, C# is the same. Regarding the
Note that a key difference between the C/C++ derived keyword See alsoReferences
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |