| FazBrowse Wikipedia Viewer | Main Page | | |
| Viewing: https://en.wikipedia.org/wiki/Final_(Java) |
In the Java programming language, the final keyword is used in several contexts to define an entity that can only be assigned once.
Once a final is called non-transitivity). This applies also to arrays, because arrays are objects; if a java.lang.System and publicclass Base{ publicvoidfoo(){ // ... } publicfinalvoidbar(){ // ... } publicstaticvoidbaz(){ // ... } publicstaticfinalvoidqux(){ // ... } } publicclass DerivedextendsBase{ // OK, overriding Base::foo() @Override publicvoidfoo(){ // ... } // forbidden @Override publicvoidbar(){ // ... } // OK, overriding Base::baz() @Override publicstaticvoidbaz(){ // ... } // forbidden @Override publicstaticvoidqux(){ // ... } }
A common misconception is that declaring a method as publicclass Sphere{ // Pi is a universal constant, about as constant as anything can be. publicstaticfinaldoublePI=3.141592653589793;// or Math.PI publicfinaldoubleradius; publicfinaldoublexPos; publicfinaldoubleyPos; publicfinaldoublezPos; Sphere(doublex,doubley,doublez,doubler){ radius=r; xPos=x; yPos=y; zPos=z; } // ... }
Any attempt to reassign radius, xPos, yPos, or zPos will result in a compile error. In fact, even if the constructor doesn't set a final variable, attempting to set it outside the constructor will result in a compilation error.
To illustrate that finality doesn't guarantee immutability: suppose we replace the three position variables with a single one:
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 in the scope of that method are accessible from within the inner class. For scalar values, once it has been assigned, the value of the final variable cannot change. For object values, the reference cannot change. This allows the Java compiler to "capture" the value of the variable at run-time and store a copy as a field in the inner class. Once the outer method has terminated and its stack frame has been removed, the original variable is gone but the inner class's private copy persists in the class's own memory.
In addition, a blank final also has to be definitely assigned before being accessed.
In C and C++, the analogous construct is the const is part of the 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 keyword, it remains unused.
C and C++ additionally use constinit for static-initialized variables.
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 final variable reference in Java. By contrast, when applying const.
In C++, the // final in a class declaration declares that a class cannot be extended classZfinal:publicX,publicY{ public: // final in a method signature declares that a method cannot be overridden further voidsomeOperation()overridefinal{ // 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 readonly
Note that a key difference between the C/C++ derived keyword const is evaluated at compile time (much like
| View original on Wikipedia | Back | FazBrowse Home |