Wednesday, December 23, 2009

Dynamic Initialization

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.

Suggested Reading:-

Class Declaration

Interface Declaration
Local,instance variable and Shadowing
Code Reuse feature of Inheritance
Why Java classes are not final

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP