Wednesday, May 30, 2018

Dynamic Initialization in JAVA

Although the preceding examples have used only constants as initializers, Java allows
variables to be initialized dynamically, using any expression valid at the time the variable
is declared.
For example, here is a short program that computes the length of the hypotenuse of
a right triangle given the lengths of its two opposing sides:
// Demonstrate dynamic initialization.
class DynInit {
public static void main(String args[]) {
double a = 3.0, b = 4.0;
// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
System.out.println("Hypotenuse is " + c);
}
}
Here, three local variables—a, b,and c—are declared. The first two, a and b, are
initialized by constants. However, c is initialized dynamically to the length of the
hypotenuse (using the Pythagorean theorem). The program uses another of Java’s
built-in methods, sqrt( ), which is a member of the Math class, to compute the square
root of its argument. The key point here is that the initialization expression may use
any element valid at the time of the initialization, including calls to methods, other
variables, or literals.

banner
Previous Post
Next Post

0 comments: