Java Variables: Local, Instance and Static, Parameter
A variable is a named storage that our programs can manipulate .
Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
You must declare all variables before they can be used.
There are four kinds of variables in Java:
*Local variables
*Object/Instance variables
*Class/static variables
These are variable types are different according to their different scope of visibility.
Local Variables:
Local variables are declared in methods, constructors, or blocks.
The scope of the local variables is withing that only i.e. local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.
There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use.
Access modifiers can be given for instance variables.
Example:
//Here ' message' is a local variable.
public class ABC{
public void printABC(){
String message="Hello";
System.out.println("Here is a message for you: "+ message);
}
}
Instance(Non-Static) Variables:
A variable that is declared inside the class but outside the method is called instance variable . It is not declared as static.
Instance variables are declared in a class, but outside a method, constructor or any block.
When a space is allocated for an object in the heap, a slot for each instance variable value is created.
These are called instance variables because their values are unique to each instance(or Object) of a class
Instance variables can be declared in class level before or after use.
Access modifiers can be given for instance variables.
Instance variables have default values.
For numbers the default value is 0, for boolean it is false and for object references it is null.
Class/static variables
A class variable is any field declared with the
Static variables are created when the program starts and destroyed when the program stops.
Static variables can be accessed by calling with the class name . ClassName.VariableName.
A variable is a named storage that our programs can manipulate .
Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
You must declare all variables before they can be used.
There are four kinds of variables in Java:
*Local variables
*Object/Instance variables
*Class/static variables
These are variable types are different according to their different scope of visibility.
Local Variables:
Local variables are declared in methods, constructors, or blocks.
The scope of the local variables is withing that only i.e. local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.
There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use.
Access modifiers can be given for instance variables.
Example:
//Here ' message' is a local variable.
public class ABC{
public void printABC(){
String message="Hello";
System.out.println("Here is a message for you: "+ message);
}
}
Instance(Non-Static) Variables:
A variable that is declared inside the class but outside the method is called instance variable . It is not declared as static.
Instance variables are declared in a class, but outside a method, constructor or any block.
When a space is allocated for an object in the heap, a slot for each instance variable value is created.
These are called instance variables because their values are unique to each instance(or Object) of a class
Instance variables can be declared in class level before or after use.
Access modifiers can be given for instance variables.
Instance variables have default values.
For numbers the default value is 0, for boolean it is false and for object references it is null.
Class/static variables
A class variable is any field declared with the
static
modifier; this tells the compiler that there is exactly one copy of
this variable in existence, regardless of how many times the class has
been instantiated Static variables are created when the program starts and destroyed when the program stops.
Static variables can be accessed by calling with the class name . ClassName.VariableName.
Comments
Post a Comment