Lecture 04 - Constructor and Method, Variables
Lecture 04 - Constructor and Method, Variables
Variables in Java:
Local variable:
Local variables are those which are declared within any block of code like methods,
constructor or initialization block. A block defines a scope. We cannot use access specifier in
local variable.
E.g. class T{
psvm(…){
int i;
}
}
Member Variable:
Member variables in a class called fields.
There are two kinds of member variable: instance and static.
Instance variable:
A variable declared inside the class but outside the body of the method, is called instance
variable. It is not declared as static. It has one copy per instance of a class.
E.g. class T{
int i;
}
Static variable:
Instance variables declared as static (global like) at the class level are, essentially, global
variables.
It has one copy per class so it saves memory.
E.g. class T{
static int i;
}