![]() |
| |
|
01.05.10 Initializing Your Java Dynamically Using Any Expression By Vaibhav Pandey There are two types of variable mainly:- 1. Instance Variable or Class Variable 2. Local Variable or method variable Instance variable are initialized by JVM to their default values if not defined explicitly. Whereas the local variables needs to be defined each time time they are declared. But the local variables can be used to a greater effect by using the concept of dynamic initialization. Dynamic Initialization can be defined as the dynamic operation that allows variables to be initialized dynamically using any expression valid at the time of the variable declaration. Above definition implies that if you need a variable to store value of an expression you can use dynamic initialization. In which value of an expression is assigned to a variable. Dynamic Initialization can be clear by understanding following example:- class DynamicInit{ public static void main(String a[]){ int a=2; int b=5; int c=a*a+b*b; System. out. println("value of c is "+c); } } Output :-The output of above program is value of c is 29 In the above program we have three variables a,b and c. Each has been declared as ' int '. Variable a and b are declared and provided values on declaration whereas the variable c has been assigned a expression to evaluate and store its value. This is done through dynamic evaluation. NOTE:-One thing which has to be remembered is that the initialization expression may use any program construct including method calls, instance variables, literals etc. Comments About the Author: Vaibhav Pandey got offered employment from an Indian Multinational IT Company. He is 21 years old. He has a huge interest in Java programming and has liked it from his study days. Vaibhav loves to blog and share his experiences and thoughts. He now resides in Lucknow, a state capital in India. Check out his blog at http://javatutorialsworld.blogspot.com. |
|
| ||
| -- JavaProNews is an iEntry, Inc. publication -- iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509 2010 iEntry, Inc. All Rights Reserved Privacy Policy Legal archives | advertising info | news headlines | free newsletters | comments/feedback | submit article |