130 likes | 146 Views
Jim Brucker. Final. final modifier. 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. public static final int MAX_LENGTH = 256;
E N D
Jim Brucker Final
final modifier 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. 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
Define a constant Constants are usually static. Variable name is uppercase. static final double NANOSECONDS = 1.0E-9;
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; }
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
"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; }
"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];
"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
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( )
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.
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
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")
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.