Рет қаралды 9
Types of variable:
1. local variable
Creating variable inside method/block is known as local variable.
Scope of local variable remains only within the method & they are temporary.
2. global variable
Creating variable outside method/block is known as global variable.
Scope of global variable remains throught the class & they are permanent.
3. class/static variable
Declaring the variable using static keyword is known as class/static variable because to access to static variable class name is used.
We can declare static variable only globally.
To access static variable from diff class we need to make use of below statement:
classname.variablename;
int o=Static_variable2.a;
static int a=11;
//globally declare
// use= main method, static, non static method
4. Instance/non-static variable
All the non-static variables are known as instance variable because to access non-static variable instance(object) need to be created.
We can use non static variable in non static method only if we want to use this variable in other class then we have to create object for that
To access non-static variable we need to make use of below statement:
-Create object of class.
-objectname.variablename
Non_static_variable o2=new Non_static_variable();
int b=o2.a;
int a=10;