Java Variable Types
A Java Variable Types name is a name given to a memory location. It is the basic unit of storage in a Java program.
- The value stored in a variable can be changed during java program execution.
- A Java Variable Types is only a name given to a memory location, all the operations (like addition of two variable or concatenation of two string) done on the variable effects that memory location.
- All the Java Variable Types must be declared before use, in Java.
Three types of variables in java :-
- local variable :- A variable declared inside the body of the method is called local variable. You can use this variable only within that method and other methods in the class also do not know that the variable exists. A local variable cannot be defined with the keyword “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 is called instance variable because its value is instance specific and is not shared between instances.
- Class/static variable :- A variable which is declared as static is called static variable. It cannot be local. You can create a single copy of static variable and share it in all the instances of the class. Memory allocation for static variable happens only once when the class is loaded into the memory.
Variable declaration :−
data type variable1 [ = value1][, variable2 [ = value2] ...] ; Example:- int a = 10, b = 20; char a = 'a';
- What are variable in Java with example?
- In Java what are static variables?
- What are instance variables in Java?
- How many types of variables in java?
- What are local variables in Java