Final variables

24 May 2008

A final variables in Java are can only be assigned once. Once they are assigned some value, they cannot be assigned some other value.

If you have final class valiable, they must be assigned to in the constructor of the class. An interesting thing is that the value of a final variable is not necessarily known at compile time. Consider the following example:

public class Sphere {
 
    public static final double PI = 3.141592653589793; 
    public final double radius;
    public final double xpos;
    public final double ypos;
    public final double zpos;
 
    Sphere(double x, double y, double z, double r) {
         radius = r;
         xpos = x;
         ypos = y;
         zpos = z;
    }
 ...
}

We have 3 final variables in the example. The first one (PI) is static as well, which allows us to access it without initiating the class.

del.icio.us:Final variables  digg:Final variables  spurl:Final variables  wists:Final variables  simpy:Final variables  newsvine:Final variables  blinklist:Final variables  furl:Final variables  reddit:Final variables  fark:Final variables  blogmarks:Final variables  Y!:Final variables  smarking:Final variables  magnolia:Final variables  segnalo:Final variables  gifttagging:Final variables

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Final variables

Leave a Reply