FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Final_(Java)

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:

final principle, the following is a legal statement:

java.awt.Point though, and this class defines its fields as public and modifiable. This means that even when reaching the origin object over an access path with only final variables, that object can still be modified, as the below example code demonstrates.

y 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 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.

finalbooleanhasTwoDigits; 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.

java.lang.reflect package contains methods that can make any field accessible and change them. As of 2026, there are plans to remove this ability in a future version of Java.

Analogous concepts

edit

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

  1. ↑ Coblenz, Michael; Sunshine, Joshua; Aldrich, Jonathan; Myers, Brad; Weber, Sam; Shull, Forrest (14–22 May 2016). "Exploring Language Support for Immutability". The 38th International Conference on Software Engineering.
  2. ↑ Java Language Specification #4.12.4
  3. ↑ "Java theory and practice: Is that your final answer?". developer.ibm.com. Archived from the original on 2009-02-08. Retrieved 2024-04-25.
  4. ↑ Java Language Specification #8.3.1.2.
  5. ↑ Flanagan, David (May 1997). "Chapter 5 Inner Classes and Other New Language Features:5.6 Other New Features of Java 1.1". Java in a Nutshell (2nd ed.). O'Reilly. ISBN 1-56592-262-X.
  6. ↑ "JEP 500: Prepare to Make Final Mean Final". openjdk.org. Retrieved 2026-01-12.
  7. ↑ What is the equivalent of Java's final in C#?

View original on Wikipedia | Back | FazBrowse Home