Basic JAVA 1
Variable in Java
Variable is name of reserved area allocated in memory.
int data=50; // Here data is variable.
Types of Variable in Java:
- Local Variable
- Instance Variables (Non-static variables)
- Class Variables (Static Variables)
1) 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.
Ex:
float getDiscount(int price)
{
float discount; //here discount is a local variable
discount=price*(20/100);
return discount;
}
2) Instance Variables:
A variable that is declared inside the class but outside the method, constructor or block is called instance variable . It is not declared as static.
Ex:
class Student
{
String name; //here name and age are
int age; // instance variable
}
3) Static variables:
A variable that is declared as static is called static variable. It cannot be local. Static variables are initialized only once. Static variables are also used in declaring constant along with final keyword.
Ex:
class Student
{
String name;
int age;
static int instituteCode=1101;
} //here instituteCode is a static variable
No comments:
Post a Comment
Hai , Post your comment . (required, Bugs, Errors )