'Blank' final fields : final « Class Definition « Java Tutorial






class A {
  private int i;

  A(int ii) {
    i = ii;
  }
}

public class MainClass {
  private final int i = 0; // Initialized final

  private final int j; // Blank final

  private final A a; // Blank final reference

  // Blank finals MUST be initialized in the constructor:
  public MainClass() {
    j = 1; // Initialize blank final
    a = new A(1); // Initialize blank final reference
  }

  public MainClass(int x) {
    j = x; // Initialize blank final
    a = new A(x); // Initialize blank final reference
  }

  public static void main(String[] args) {
    new MainClass();
    new MainClass(47);
  }
}








5.27.final
5.27.1.final Variables
5.27.2.'Blank' final fields
5.27.3.Java Final variable: Once created and initialized, its value can not be changed
5.27.4.Using 'final' with method arguments
5.27.5.The effect of final on fields
5.27.6.You can override a private or private final method
5.27.7.Making an entire class final
5.27.8.Demonstrates how final variables are replaced at compilation time
5.27.9.Demonstration of final class members
5.27.10.Base class for demonstation of final methods
5.27.11.Demonstration of final constants
5.27.12.Demonstration of final variables