Wednesday, 4 January 2012

Types of Variables in Java


Types of Variables:

There are various variables on the basis of scope in Java. They are:
1.       Class Variables;
2.       Instance Variables;
3.       Local Variables;
4.       Static Variables;
5.       Automatic Variables;

Class Variables:
-          These are accessible with in a class and its object.
-          These must be declared inside the class before they are used.

Instance Variables:
-          These are declared inside a class and are created when the class is instantiated.
-          Object gives different values to these instance variables.

Local Variables:
-          They are declared inside the methods.
-          Their scope is within the block of code in which they are defined.
-          They can’t be accessed outside the code.

Static Variables:
-          These variables have memory allocated only once, but they are globally accessed through all instances of the class.
-          Even if the instance is destroyed, these variables are not destroyed.

Automatic Variables:
-          These are accessible within the function in which they are declared.
-          The automatic variables are created when a function is called and are destroyed when we exit the function.

*       For allocation of memory, we use new.
*       For DE allocation of memory, we use Garbage Collector.

No comments:

Post a Comment