Submit Your Article
Home Articles News Tutorials Videos Add An Article
Topics: Design Photoshop Programming PHP CSS Java Database Web Development Javascript Ajax
– Close + Open

Find Out More About DevWebPro!

Sign up for the newsletter


» Terms & Conditions

Welcome to the New DevWebPro!

DevWebPro Includes:
  Hundreds Of Tutorials   Developer News
  Unique Gadget Videos   Tons of Topics to Discuss
  Expert Advice   We Will Publish Your Articles

Initializing Your Java Dynamically Using Any Expression

By: Vaibhav Pandey
Tuesday, January 5th, 2010
Text: Decrease Font Size Increase Font Size | Print Print Article | Share: Delicious Digg StumbleUpon Post to Twitter Post to Facebook

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

Topics: ,

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.
DevWebPro is an iEntry Network ® publication - © 1998-2010 All Rights Reserved