Sunday, December 18, 2011

Initializing Constants in Java

Java doesn't use the const keyword for declaring constants (like C, C++ or C#). Instead, it uses the final keyword, which in combination with the static keyword (or not as you will see below) can provide you a constant variable (which is a bit of an oxymoron, but we'll take it as it is).

If a final variable is not static, it can be initialized either by using static initialization or an assignment statement inside the constructor. If the variable is static, it can only be initialized by using static initialization. A non-static final variable who is being initialized in the constructor is called a blank variable (or blank constant).

To understand this better, check out the example below:
public class ConstantTestClass 
{
 private final int myFinalData;    //Blank constant
 private final static int myStaticAndFinalData = 4; //Static initialization
 
 public ConstantTestClass()
 {
  myFinalData = 3; //Constructor initialization of constant
 }
 
 public static void main(String args[])
 {
  ConstantTestClass ctc = new ConstantTestClass();
  System.out.println(ctc.myFinalData +"\n" 
    + ConstantTestClass.myStaticAndFinalData);
 }
 //Output:
 //3
 //4
}
The code snippet above will not trigger any error when compiled (since it's legal to initialize a final "variable" inside a constructor if the variable is not static). At compilation, the class would trigger an error if the final variable wouldn't be initialized by the constructor.

The beauty of the initialization inside of the constructor is that you can initialize your non-static constant with a value that you do not know at compile-time. See the example below for better understanding:
public class ConstantTestClass 
{
 private final int myFinalData;      //Blank constant
 private final static int myStaticAndFinalData = 4; //Static initialization
 
 public ConstantTestClass(int x,int y)
 {
  myFinalData = x+y; //Constructor initialization of constant
 }
 
 public static void main(String args[])
 {
  int x = (int)Math.sqrt(1);   //Unknown value at compile time
  int y = (int)(Math.PI - 1);  //Unknown value at compile time
  ConstantTestClass ctc = new ConstantTestClass(x,y);
  System.out.println(ctc.myFinalData +"\n" 
         + ConstantTestClass.myStaticAndFinalData);
 }
 //Output:
 //3
 //4
}
Also, we could initialize myFinalData using a static initializer and obtain the same results as in the first example. The only disadvantage is that you must know at compile-time the value that should be assigned to your constant. 
public class ConstantTestClass 
{
 private final int myFinalData = 3; //Static initialization  
 private final static int myStaticAndFinalData = 4; //Static initialization
 
 public ConstantTestClass(){}
 
 public static void main(String args[])
 {
  ConstantTestClass ctc = new ConstantTestClass();
  System.out.println(ctc.myFinalData +"\n" 
    + ConstantTestClass.myStaticAndFinalData);
 }
 //Output:
 //3
 //4
}
Also, one must note that if your constant is applicable to all object of your class you should probably make it static in most cases.

1 comment:

  1. Additionally I have seen many applications use interfaces for defining constants as variables inside an interface are always public static final. Thought may be this could help someone coming here.
    Thanks
    Initializing final variables in java

    ReplyDelete

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger