One thing I knew that final variables have to be initialized in very much the same line you declare them. So I embarked to see how things could go actually, rather than hearsay literature.So, following are a few code snippets that I tried, and obviously now, the first line of the paragraph is not absolutely correct.
Note: "final" variables are not always constants, as their values might not be available during compile time, and may be taken whilst the application is running.
If you have any comments or improvements, please post in the comments section.
1.
package finalized; /* * This code shows initialization of instance final variable * using constructors. This means that each object has its own * copy of final instance variable. */ public class FinalDemo{ public final int i; FinalDemo(){ i = 10; } FinalDemo(int var){ i = var; } public static void main(String... args){ FinalDemo fd1 = new FinalDemo(); FinalDemo fd2 = new FinalDemo(17); //fd1.i = 30; //This is not allowed as "i" is final for this object. } }
2.
package finalized; /* * This code shows that even though the StringBuffer reference * variable is made final, the contents of the object can still * be manipulated, as the object is not immutable like String. * But the reference variable can not be made to point to a new * object. Hence, the reference is fixed, the contents are not! */ public class DoHellWithFinal { final int j; final StringBuffer sb = new StringBuffer(); //instance block { j = 20; sb.append("God is "); } //Constructor public DoHellWithFinal() { sb.append("Gracious!"); //j = 35; //not possible as "j" is already initialized //in the initializer block. //sb = new StringBuffer("trying to reassign object"); //will give compilation error } public DoHellWithFinal(String str){ sb.append(str); } public static void main(String...args) { DoHellWithFinal do1 = new DoHellWithFinal(); System.out.println("j = " + do1.j); System.out.println("do1.sb = " + do1.sb); DoHellWithFinal do2 = new DoHellWithFinal("my friend!"); System.out.println("do2.sb = " + do2.sb); } }
Output is:
j = 20
do1.sb = God is Gracious!
do2.sb = God is my friend!
A final instance variable can be initialized using any ONE of the following 3 mechanisms:
ReplyDelete1. Declaration - final instance variables can be initialized as part of variable declaration.
OR
2. Constructor(s): If using constructor(s) to initialize final instance variable then we need to initialize it in ALL the constructors
3. Instance Initializer(s): If using instance initializer to initialize final instance variables then we need to initialize in ONLY ONE of the instance initializers. Note: A class can have any number of (ie, multiple) instance initializers.
A final static variable can be initialized using any ONE of the following two mechanisms:
1. At the point of declaration - A static final variable can be initialized at the point of declaration.
2. Class initializer (ie, static block) - If using static block mechanism to initialize final static variable then it can be initialized in ONLY ONE of the static blocks. Note: A class can have any number of (ie, multiple) static blocks.
A final local variable should be initialized before it is used. In other words, a final local variable need not be initialized at the point of declaration, moreover, if the final local variable is not used at all then it need not even be initialized. This is different from final instance and final static variables which must be initialized by the time object creation completes and by the time class loading completes respectively.
A final method parameter is initialized when the method is invoked. A final method parameter cannot be initialized in the method body.
@Amar Gunda, Thank you for elaborating. "A final method parameter is initialized when the method is invoked. A final method parameter cannot be initialized in the method body." I never knew that you could declare a variable final in parameter, awesome info. :)
ReplyDelete