Monday, April 21, 2014

Non-Access Modifier- Final, Part1.

In Java final plays a vital role whenever we want to declare some constant in our code.
We can use Final keyword with following:
  • class
  • instance variable
  • instance methods
  • reference variables
  • static variables
  • static methods
  • local variables 
  • parameters(inside methods)
In all these scenario, it imposes some restriction with their usage, Here in this series I will cover all different scenarios.

Scenario 1: when we need to use some constant value repeatedly 

package in.technoscience.finalasconstant.Before;
// here i have not used the final keyword
public class FinalTheConstant {
 
 public static class ToolsForCircle{
  
  public double getCircleArea(final double radius){
   return (Math.pow(radius, 2)*3.141);
  }
  
  public double getCircumfere(final double radius){
   return ((radius*2)*3.14);
  }
  
  public double getVolume(final double radius, final double height){
   return ((radius*2*height)*3.141);
  }
 }

}

Problem: The problem with this code is that the developer has to change all three instances of the value 3.141 in all three methods if he wants to make his calculations more precise.

Improvement:
package in.technoscience.finalasconstant.After;

public class FinalAsConstant {
 public static class BetterCircleTool{
  
  // value of pi as constant using final
  public static final double pi=3.14;
  
  public double getCircleArea(final double radius){
   return (Math.pow(radius, 2)*pi);
  }
  
  public double getCircumfere(final double radius){
   return ((radius*2)*pi);
  }
  
  public double getVolume(final double radius, final double height){
   return ((radius*2*height)*pi);
  }
 }

}
Now the developer can change the constant, and this one change will propagate throughout the class

Scenario2: How compiler reacts when it sees the final
package in.technoscience.finalasconstant.Before;

public class Color {
 int red;
 int green;
 int black;
 public Color(int red, int green, int black) {
  super();
  this.red = red;
  this.green = green;
  this.black = black;
 }
 

}
Different type of instance variable declared as final
package in.technoscience.finalasconstant.Before;

public class ReplacementForFinal {

 /** A string constant */

 public final static String A_STRING = "I Am Java Developer";

 /** An int constant. */

 public final static int AN_INT_Type = 51;

 /** A double constant. */

 public final static double A_DOUBLE_Type = 12.55d;

 /** An array constant. */

 public final static int[] AN_ARRAY_Type = new int[] { 1, 2, 3, 6, 9, 18, 36 };

 /** A color constant. */

 public final static Color A_COLOR = new Color(45, 0, 155);

 public void someMethod() {

  System.out.println(A_STRING);

  System.out.println(AN_INT_Type);

  System.out.println(A_DOUBLE_Type);

  System.out.println(AN_ARRAY_Type);

  System.out.println(A_COLOR);

 }

}

Compiler reaction:
it starts substituting out the primitives and Strings objects as below:
package in.technoscience.finalasconstant.Before;

public class ReplacementForFinal {

 /** A string constant */

 public final static String A_STRING = "I Am Java Developer";

 /** An int constant. */

 public final static int AN_INT_Type = 51;

 /** A double constant. */

 public final static double A_DOUBLE_Type = 12.55d;

 /** An array constant. */

 public final static int[] AN_ARRAY_Type = new int[] { 1, 2, 3, 6, 9, 18, 36 };

 /** A color constant. */

 public final static Color A_COLOR = new Color(45, 0, 155);

 public void someMethod() {

  System.out.println("I Am Java Developer");

  System.out.println(51);

  System.out.println(12.55d);

  System.out.println(AN_ARRAY_Type);

  System.out.println(A_COLOR);

 }

}

Note: 
As we have written whole code is all in one class, if we change a constant, we have to recompile this class anyway. However, if another class (for example, TheClient) is using the constant A_STRING and you change it in ReplacementForFinal then we have a problem. TheClient will have to be recompiled to trigger a re-substitution using the new A_STRING value, but the Java compiler will not notice this dependency.

package in.technoscience.finalasconstant.Before;

public class TheClient {
 public static void main(String[] args) {
  System.out.println("i wish to be::" + ReplacementForFinal.A_STRING);
 }
}

More scenarios on next post final part2

2 comments:

  1. Good try. Will help beginners

    ReplyDelete
    Replies
    1. Thank you for appreciation, As i am new in blogging so trying to help beginners first

      Delete