A variable is a storage area where we store the data and the information. A variable is a holder which holds the value while the java program is executed. Variable also declare with a data type. When we declare a variable name the first letter of the variable should not be a digit. And the variable name should not be a keyword.
String name; |
Here string is a data type and name is a variable of this data type.
A local variable always declares inside the body of the method and function. A local variable can be a static variable. Local variable should be initialized before accessing them.
Class Test |
Output
Sum = 50 |
A variable that is declared inside a class but outside the body, method, constructor or block, is called instance variable. Instance variable need not be initialized before using them as they are automatically initialized to their default values.
class Test |
Output:-
Sum =30 |
A variable which is declared with the static keyword, Static variable is stored in the static memory. A static variable is regular to every one of the occasions (or objects) of the class since it is a class-level variable. you can say that only a single copy of the static variable is created and shared among all the instances of the class. Memory designation for such factors possibly happens once when the class is loaded in the memory.
Static variable Syntax
static keyword followed by data type and variable name
static data_type variable_name; |
Static variable example in Java
class VariableDemo |
Output
Obj1: count is=2 |
As should be obvious in the above model that both the items are having a similar duplicate of static variable that is the reason, they shown a similar estimation of check.