Download presentation
Presentation is loading. Please wait.
1
Final Jim Brucker
2
final modifier public static final int MAX_LENGTH = 256; private final String name; public void public final String getName( ) {return name;} public final class Double extends Number A final variable cannot be modified after first assignment. A final parameter cannot be reassigned (*). A final method cannot be overridden in a subclass. A final class cannot have a subclass.
3
Define a constant Constants are usually static.
Variable name is uppercase. static final double NANOSECONDS = 1.0E-9;
4
Define an unchangable attribute
Use final for attributes that should not change after object is created. 1. Must assign a value in declaration or in constructor. 2. Use camelCase names. public class Purse { // purse capacity cannot be changed! private final int capacity; Purse(int capacity) { this.capacity = capacity; }
5
A parameter that should not change
Use final to indicate that a parameter or local variable should not change. code safety safely share variable among threads allows some code optimization by compiler // Run a task in a separate thread public void runInBackground( final Runnable task) { Thread t = new Thread(task); t.start(); // start a separate thread // returns immediately, without waiting
6
"final" is sometimes required
For anonymous classes and threads, "final" may be needed for variables from the surrounding scope that are used in anonymous class or thread. Example: method uses the parameter (message) to define an anonymous class. This won't compile in Java 7 (or before) unless message is "final". public Runnable makeMessageTask(String message) { Runnable task = new Runnable() { public void run(){ System.out.println(message); }}; return task; }
7
"final" applies to reference, but ...
You can still call mutator methods that modify attributes of an object, even though the reference is final. You can modify elements of a "final" array, too. final Date birthday = new Date(100,Calendar.JULY,1); // Only the reference is final! Object can change. birthday.setMonth(Calendar.JANUARY); final String [] s = {"This", "is", "final", "!"}; // that's what you think... s[3] = "or maybe not."; // ERROR, s is final s = new String[10];
8
"final" method cannot be overridden
A final method may not be overridden by a subclass. public class Person { private long id; public final long getId() { return id; } } public class Student extends Person { public long getId() { return studentId; }//ERROR
9
Why declare a method final?
1. Method contains essential logic that subclasses must not change. public class Object public final void notify( ) Wake up a thread that is waiting for this object. public final void wait( ) Wait for another thread to invoke notify( )
10
Why declare a method final?
2. Efficiency. final methods cannot be overridden, so they are not a target for polymorphism. Compiler can use static binding (*) of final methods, which is more efficient at run-time. (*) binding - connect a method name to the actual method code.
11
final class cannot have a subclass
A final class may not have subclasses. Methods in a final class are implicitly final. Some final classes: String, StringBuilder, Double, Integer public class MyString extends String ERROR - String class is final
12
Why Declare a Class as final?
1. Class is essential to correct functioning of other parts of the API or (for Java) the JVM. We don't want a subclass to screw up the logic! Strings are essential to Java. Java has special handling of Strings. concatenation: "hello, " + user string pool for constants conversion of String to/from other types, such as Double.parseDouble("2.4")
13
Why Declare a Class as final?
2. Efficiency. All methods are final, therefore the compiler can perform static binding (no polymorphism) which results in faster run-time. String methods are invoked a lot. They need to be fast. StringBuilder is used a lot to build strings. Its methods need to be fast, too.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.