Java/Class/Final
Содержание
Blank final fields
<source lang="java">
// : c06:BlankFinal.java // "Blank" final fields. // From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002 // www.BruceEckel.ru. See copyright notice in CopyRight.txt. class Poppet {
private int i; Poppet(int ii) { i = ii; }
} public class BlankFinal {
private final int i = 0; // Initialized final private final int j; // Blank final private final Poppet p; // Blank final reference // Blank finals MUST be initialized in the constructor: public BlankFinal() { j = 1; // Initialize blank final p = new Poppet(1); // Initialize blank final reference } public BlankFinal(int x) { j = x; // Initialize blank final p = new Poppet(x); // Initialize blank final reference } public static void main(String[] args) { new BlankFinal(); new BlankFinal(47); }
} ///:~
</source>
Experiment with final args to functions
<source lang="java">
/*
* Copyright (c) Ian F. Darwin, http://www.darwinsys.ru/, 1996-2002. * All rights reserved. Software written by Ian F. Darwin and others. * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS"" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * Java, the Duke mascot, and all variants of Sun"s Java "steaming coffee * cup" logo are trademarks of Sun Microsystems. Sun"s, and James Gosling"s, * pioneering role in inventing and promulgating (and standardizing) the Java * language and environment is gratefully acknowledged. * * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for * inventing predecessor languages C and C++ is also gratefully acknowledged. */
import java.util.*; /**
* Experiment with "final" args to functions (new in 1.1) */
public class FinalArgs {
public static void main(String argv[]) { new FinalArgs().run(); } void run() { System.out.println("Hummm de dummm..."); myFunc(Calendar.getInstance()); System.out.println("Once upon a time..."); } void myFunc(final Calendar d) { // d = null; // this will not compile d.set(Calendar.YEAR, 1999); // this will compile, and changes the object }
}
</source>
Java Final variable: Once created and initialized, its value can not be changed
<source lang="java">
public class Main {
public static void main(String[] args) { final int hoursInDay = 24; System.out.println("Hours in a day = " + hoursInDay ); }
} //Hours in a day = 24
</source>
Making an entire class final
<source lang="java">
// : c06:Jurassic.java // Making an entire class final. // From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002 // www.BruceEckel.ru. See copyright notice in CopyRight.txt. class SmallBrain { } final class Dinosaur {
int i = 7; int j = 1; SmallBrain x = new SmallBrain(); void f() { }
} //! class Further extends Dinosaur {} // error: Cannot extend final class "Dinosaur" public class Jurassic {
public static void main(String[] args) { Dinosaur n = new Dinosaur(); n.f(); n.i = 40; n.j++; }
} ///:~
</source>
The effect of final on fields
<source lang="java">
// : c06:FinalData.java // The effect of final on fields. // From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002 // www.BruceEckel.ru. See copyright notice in CopyRight.txt. import java.util.Random; class Value {
int i; // Package access public Value(int i) { this.i = i; }
} public class FinalData {
private static Random rand = new Random(); private String id; public FinalData(String id) { this.id = id; } // Can be compile-time constants: private final int VAL_ONE = 9; private static final int VAL_TWO = 99; // Typical public constant: public static final int VAL_THREE = 39; // Cannot be compile-time constants: private final int i4 = rand.nextInt(20); static final int i5 = rand.nextInt(20); private Value v1 = new Value(11); private final Value v2 = new Value(22); private static final Value v3 = new Value(33); // Arrays: private final int[] a = { 1, 2, 3, 4, 5, 6 }; public String toString() { return id + ": " + "i4 = " + i4 + ", i5 = " + i5; } public static void main(String[] args) { FinalData fd1 = new FinalData("fd1"); //! fd1.VAL_ONE++; // Error: can"t change value fd1.v2.i++; // Object isn"t constant! fd1.v1 = new Value(9); // OK -- not final for (int i = 0; i < fd1.a.length; i++) fd1.a[i]++; // Object isn"t constant! //! fd1.v2 = new Value(0); // Error: Can"t //! fd1.v3 = new Value(1); // change reference //! fd1.a = new int[3]; System.out.println(fd1); System.out.println("Creating new FinalData"); FinalData fd2 = new FinalData("fd2"); System.out.println(fd1); System.out.println(fd2); }
} ///:~
</source>
Using final with method arguments
<source lang="java">
// : c06:FinalArguments.java // Using final with method arguments. // From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002 // www.BruceEckel.ru. See copyright notice in CopyRight.txt. class Gizmo {
public void spin() { }
} public class FinalArguments {
void with(final Gizmo g) { //! g = new Gizmo(); // Illegal -- g is final } void without(Gizmo g) { g = new Gizmo(); // OK -- g not final g.spin(); } // void f(final int i) { i++; } // Can"t change // You can only read from a final primitive: int g(final int i) { return i + 1; } public static void main(String[] args) { FinalArguments bf = new FinalArguments(); bf.without(null); bf.with(null); }
} ///:~
</source>