1. Overview
In this tutorial, We will be discussing variables dynamic initialization topic with example programs in java. Most of the developers do this but do not realize this is said to be dynamic initialization. In the previous tutorial, we discussed in-depth on variables in java. If you are an experienced developer, continue reading this article. Otherwise, please go through this to get a complete understanding. Accessing Local variables from lambda is quite different and that will cause compile-time errors if you are using variables inside lambda expressions.
If any variable is not assigned with value at compile-time and assigned at run time is called dynamic initialization of a variable. Basically, this is achieved through constructors, setter methods, normal methods and builtin api methods which returns a value or object.
2. Java Program to variables dynamic initialization
Let us see the few examples of how to initialize variables at run time.
Creating a class with the only constructor.
class Rectangle { private double length; private double width; public Rectangle(double length, double width) { super(); this.length = length; this.width = width; } }
Constructor based dynamic initialization:
Writing the main class to initialize the length and width variable at runtime.
package com.javaprogramto.engineering.programs.variables; /** * * Variables Dynamic Initialization Constructors * * @author Venkatesh javaprogramto.com * */ public class VariablesDynamicInitializationConstructors { public static void main(String[] args) { // passing the values at runtime. Rectangle rectangle1 = new Rectangle(10, 15); Rectangle rectangle2 = new Rectangle(5.5, 7.5); Rectangle rectangle3 = new Rectangle(2.9, 10); } }
in the above program, length and width are initialized at runtime with different values for each Rectangle object.
Let us do now with setter methods. Now add the following to the Rectangle class.
Setter methods based dynamic initialization:
Let us do now with setter methods. Now add the following to the Rectangle class.
public Rectangle() { } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; }
Now, values can be set at runtime using setter methods.
Rectangle rectangle4 = new Rectangle(); rectangle4.setLength(20); rectangle4.setWidth(30);
Normal method to initialize a variable at runtime:
Adding a method to calculate the area of the Rectangle to the Rectangle class and returns a double value.
public double findArea() { return this.length * this.width; }
Adding code in main method.
Rectangle rectangle5 = new Rectangle(); rectangle5.setLength(50); rectangle5.setWidth(20); double rectArea = rectangle5.findArea(); System.out.println("Rectangle 5 area : " + rectArea);
rectArea double variable is initialized with value at runtime when findArea() is invoked.
Using Built-in Methods to initialize a variable dynamically:
Java Math API has utility static methods that return values.
int sum = Math.addExact(10, 20); System.out.println("Sum : " + sum); int sub = Math.subtractExact(20, 10); System.out.println("Substract : " + sub);
In the above program, sum and sub-variables are initialized dynamically at runtime.
In this article, you have seen how to initialize a variable dynamically at runtime using constructors, setter methods, normal methods and API methods which returns values.
3. Conclusion
In this article, you have seen how to initialize a variable dynamically at runtime using constructors, setter methods, normal methods and API methods which returns values.
No comments:
Post a Comment
Please do not add any spam links in the comments section.