Final variables
24 May 2008A 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.
Related Posts:
- Benefits of using Final class
- Using static and final attributes – An example (I)
- Error codes lookup - II
- Fetching parms form web.xml - I
- The Observer Design Pattern(II)
- Using static and final attributes – An example (II)
- Entity (Tips/Info)
- Creating J2ME MIDlet Project - II
- Finally block - II
- Error codes lookup - I
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