![]() |
| Variables in Java Language |
Variables in Java: Variable is a name of storage space which is used to store data. Variable value may be changed. Variables are containers that store data values.
You can put specific types of information, like numbers, text, or true/false values. Data types define what kind of data a variable can hold.
Variables can only hold whole numbers, while others can hold text or decimal numbers. A variable is like a placeholder or a name given to a memory location where you can store a value.
How to Declaration Variable in Java
float marks; char grade; |
Here int is a variable of type int or marks is a variable of type float and grade is a variable of type char.
How to Initialization of Variable in Java
float marks=98.6; char grade='A'; |
Here 100 is the value of id or 98.6 is the value of marks and A is the value of grade. Character value is always written in single quotes.
Rules if Declaring variables in Java
1.The first letter of variable should be alphabet or underscore(_).
2. The first letter of variable should not be a digit
3. After first character it may be combination of alphabets and digits.
4. Blank space are not allowed in variable name.
5. Variable name should not be a keyword.
6.Variable name are case sensitive like JIOCODING or jiocoding are different variables name.
Types of Variables in Java
- Local Variable
- Instance Variable
- Static Variable
| Variable Type | Declared in | Memory | Initialization |
|---|---|---|---|
| Local Variable | Block | Stack | Yes |
| Instance Variable | Class | Heap | No |
| Static Variable | Static(Class) | Method Area | No |
Local Variable in Java
Local Variable: A variable declared inside the body of the method or constructor or block is called local variable.
Local variable can be used only inside that function in which it is declared. A local variable can be a static variable.
Example of Local Variable in Java
public void addition(){ int x=10; //Local Variable int y=50; //Local Variable System.out.println("Addition of x+y:"+(x+y)); } public static void main (String[] args){ jio.addition(); } ============OUTPUT============ Addition of x+y:60 |
What is Instance Variable in Java
Instance Variable: A variable which is declared inside a class but outside the body of the method or constructor or block is called instance variable. Instance variable can be used anywhere in the program.
Example of Instance Variable in Java
void display() { System.out.println("Website Name: " + name); } } public class Main { public static void main(String[] args) { Jiocoding j = new Jiocoding(); j.name = "Jiocoding"; s1.display(); } } ============OUTPUT============ Website Name: Jiocoding |
What is Static Variable in Java
Static Variable: A variable which is declared with static keyword, inside a class but outside the body of the method or constructor or block is called static variable.
Static variable is stored in the static memory. Static variables are created when the program starts and destroyed when the program stops. Static variable can be called by class name directly.
Example of Without Static Variable in Java
| class Jiocoding{ public static void main(String[] args) { int i; for(i=0;i<=5;i++){ int x=0; System.out.println(x++); } } } ============OUTPUT============ Without Static Variable:0 Without Static Variable:0 Without Static Variable:0 Without Static Variable:0 Without Static Variable:0 |
Example of Static Variable in Java
public static void main(String[] args) { for(int i=0;i<=5;i++){ System.out.println("With Static Variable:"+x++); } } } ============OUTPUT============ With Static Variable:2 With Static Variable:3 With Static Variable:4 With Static Variable:5 |

0 Comments