Wednesday 13 January 2016

VARIABLES IN JAVA

Java Programming language defines three kind of variables.
  1. Instance variables
  2. Static Variables
  3. Local Variables
1.Instance Variables
Instance variables are variables that are declare inside a class but outside any method,constructor or block. Instance variable are also variable of object commonly known as field or property.
example:
class Student 
{
String name;
int age;
}
Here name and age are instance variable of Student class.

2.Static Variables
Static are class variables declared with static keyword. Static variables are initialized only once. Static variables are also used in declaring constant along with final keyword.
example:
class Student 
{
String name;
int age;
static int branchCode=112;
}
Here branchCode is a static variable. Each object of Student class will share branchCode property.

3.Local Variables
Local variables are declared in method constructor or blocks. Local variables are initialized when method or constructor block start and will be destroyed once its end. Local variable reside in stack. Access modifiers are not used for local variable.
float getDiscount(int mrp)
{
float discount;
discount=mrp*(10/100);
}
Here discount is a local variable.

No comments:

Post a Comment