If you have a final reference to a Java object, you can still manipulate it, but you cannot change its reference. For example, this code is completely legal:
import javax.swing.JLabel; class Test1 { private final static JLabel l = new JLabel("Old text"); public static void main(String[] args) { System.err.println(l.getText()); l.setText("New Text"); System.err.println(l.getText()); } }
But you cannot say:
l = new JLabel("Newest Text");
After the first assignment l. Please note that you can do this though:
import javax.swing.JLabel; class Test1 { public static void main(String[] args) { final JLabel l; String s = getArbitaryString();
This can be done because when l is declared, it is not assigned to anything not even null. Thus, you are allowed to assign him something only once.
The same goes for primitives. You can assign it a value as follows:
class Test1 { public static void main(String[] args) { final int i; i = 2; } }
But now you cannot manipulate it further, because the only thing you can do for primitive types is to assign values ββto them.
Savvas Dalkitsis Aug 08 '09 at 21:17 2009-08-08 21:17
source share