Home : Course Map : Chapter 5 : Java :
final Modifier & Constants
JavaTech
Course Map
Chapter 5
File/Package/Import
  Demo 1
  Demo 2
Access/Visibility
final & Constants
Static Import
Jar Files
  Demo 3
Applet Directories
3rd Party Packages
CLASSPATH
javadoc
CodingConventions
Exercises

    Supplements
Scope
Debug Techniques
Java Runtime
Class Class
JVM Instructions 2
JVM Processing
pack200

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The final modifier indicates that a data field cannot be modified, as in

    final double PI = 3.14;

Any attempt to assigne PI to a new value will result in a runtime error. Thus final makes data useful as constants.

Often such constants are needed by other classes, so they are declared static as well, as in

  public class MyMath
  {
    public final static double TWO_PI = 6.28;
    ...
  }

Then other classes can then reference it, as in

    ...
    double y = theta / MyMath.TWO_PI;
    ...

final Methods

The final modifier is also used with methods to indicate that they cannot be overriden by subclasses.

  public class MyMath
  {
    ...
    public final double MyFormula() {
      ...
    }
  }

This helps to improve their performance since the JVM will not need to check for overriding versions with the method is invoked.

Latest update: Oct. 22, 2004

            Tech
DecimalFormat
  Demo 1
  Demo 2

System.out.printf
  Demo 3
CJ Format Class
  Demo 4   Demo 5
Other Format Tools

Immutable Complex
Exercises

           Physics
Interpolation
Integration
  Demo 1
Simpson Rule
  Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.