...
Code Block |
---|
|
class Test{
Â
 Test 
 Test(int a, int b){
  this  this.a = a;
  this  this.b = b;
  }
 void void set_ab(int a, int b){
  this  this.a = a;
  this  this.b = b;
  }
 void void print_ab(){
  System  System.out.println("the value a is: "+this.a);
  System  System.out.println("the value b is: "+this.b);
  }
 private private int a;
 private private int b;
}
public class TestFinal1 {
Â
 public 
 public static void main(String[] args) {
               final Test mytest = new Test(1,2);
               mytest.print_ab();
               //now we change the value of a,b.
               mytest.set_ab(5, 6);
               mytest.print_ab();
      Â
          
    }
}
|
Non-Compliant Code Example
If you do not want to change a and b after they are initialized, the simplest approach is to declare a and b final:_
Code Block |
---|
|
 void void set_ab(int a, int b){ //But now compiler complains about set_ab method!
  this  this.a = a;
  this  this.b = b;
  }
 private private final int a;
 private private final int b;
|
But now you can not have setter methods of a and b.
...
Code Block |
---|
|
class NewFinal implements Cloneable
{
 NewFinal NewFinal(int a, int b){
  this  this.a = a;
  this  this.b = b;
  }
 void void print_ab(){
  System  System.out.println("the value a is: "+this.a);
  System  System.out.println("the value b is: "+this.b);
  }
 void void set_ab(int a, int b){
  this  this.a = a;
  this  this.b = b;
  }
 public public NewFinal clone() throws CloneNotSupportedException{
  NewFinal  NewFinal cloned = (NewFinal) super.clone();
  return  return cloned;
  }
 private private int a;
 private private int b;
}
public class Test2 {
Â
 public 
 public static void main(String[] args) {
               final NewFinal mytest = new NewFinal(1,2);
               mytest.print_ab();
               //get the copy of original object
   try   try {
   NewFinal mytest2 = mytest.clone();
               //now we change the value of a,b of the copy.
    mytest2 mytest2.set_ab(5, 6);
     //but the original value will not be changed
               mytest.print_ab();
    } catch (CloneNotSupportedException e) {
      // TODO Auto-generated catch block
   e   e.printStackTrace();
    }
  }
}
|
One common mistake about this is to use public static final array, clients will be able to modify the contents of the array (although they will not be able to change the array itself, as it is final).
...
Wiki Markup |
---|
With this declaration, {{SOMETHINGS\[1\]}}, etc. can be modified by clients of the code. |
Code Block |
---|
|
 public public static final SomeType [] SOMETHINGS = { ... };
|
...
Code Block |
---|
|
private static final SomeType [] SOMETHINGS = { ... };
public static final SomeType [] somethings() {
   return SOMETHINGS.clone();
}
|
...
Code Block |
---|
|
private static final SomeType [] THE_THINGS = { ... };
public static final List<SomeType> SOMETHINGS =
   Collections.unmodifiableList(Arrays.asList(THE_THINGS));
|
...
Chapter 6, Core Java⢠2 Volume I - Fundamentals, Seventh Edition by Cay S. Horstmann, Gary Cornell
Publisher:Prentice Hall PTR;Pub Date:August 17, 2004
...
OBJ02-J. Avoid using finalizers 06. Objects Orientation (OBJ) OBJ31-J. Misusing public static variables