Variable :
Variable is an identifier which is used to identify a particular memory location. If we want to store the value in the memory (or) retrieve the value from the memory we have to use VariableName
Syntax to declare a Variable
datatype variablename;
Type's Of Variable
- Static Variable
- Instance Variable
- Method Parameter
- Local Variable
1.) Static Variable
- Static Variable must declare with static keywords
- Static Variable must be declare with in the class and outside method with static keyword
class A
{
static int x;
void show()
{
// Code
}
}
2.) Instance Variable
- Instance variable will allocated a separated memory for every object
- Instance variable must declare inside the class and outside the method.
class A
{
int x;
void show()
{
// Code
}
}
3.) Method Parameter
- Method Parameter must be declare with in the method parenthesis
- Method Parameter must be declare, if we want to pass the values for the variables at the time of calling the method then declare Method parenthesis
- Method Parameter must be initialized at the time of Calling Method
- the memory of Method Parameter is allocated on "STACK".
- the memory is allocated at the time of Calling Method
- the scope of Method Parameters is with in the Method.
- the lifespan of Method Parameter is until the method is live
4.) Local Variable
- the variable that was declare with in the method are called "Local Variable"
- Local Variable must be declare with in the Block | Method
class A
{
int x;
int y;
void Add()
{
int z = x+y; (here z is Local Variable )
}
}
class A
{
void show()
{
int x;
int y=0;
int z;
z=20;
}
1 Comments
Detailed description.
ReplyDeleteVery helpful