Variable is a data container that saves the data values during Java program execution is knows as Variable. Variable is assign a with the data type. A variable is a memory location name for data. A Variable is a combination of “vary + able” which means its value can be change.
Types of Variable
There are three type of Variable in JAVA Programming
- Local Variable
- Instance Variable
- Static Variable
Local Variable in JAVA Programming
A Local variable declared inside the body of the method is knows as local variable in JAVA. A Local variables are declare in methods, constructors, or blocks. Local variables are create when the method, constructor or block is enter and the variable will be destroy once it exits the method, constructor, or block.
Example Of Local Variable
Input Value
Local Variable in JAVApublic class local { public void coffee() { int price = 42; price = price + 10; System.out.println("coffee price is : " + price); } public static void main(String args[]) { local test = new local(); test.coffee(); } }
Let’s Run the code
Output Value
Instance Variable in JAVA Programming
An Instance variable declared inside the class but outside the body of the method is knows as an instance variable.
Example of Instance Variable in JAVA Programming
Input Value
Instance Variable in JAVApublic class instance { public String name; private double salary; public instance (String empName) { name = empName; } public void setSalary(double empSal) { salary = empSal; } public void printEmp() { System.out.println("name : " + name ); System.out.println("salary :" + salary); } public static void main(String args[]) { instance empOne = new instance("rahul"); empOne.setSalary(5000); empOne.printEmp(); } }
Let’s Run the code
Output Value
Static Variables in JAVA Programming
A Static variable that is declare as static is knows as static variable. Static variables are store in the static memory.
Example of Static Variable in JAVA
Static Variables in JAVA Programmingpublic class staticsvariable { private static double milkprice; public static final String home = "cow milk "; public static void main(String args[]) { milkprice = 50000; System.out.println(home + "price:" + milkprice); } }
Let’s Run the code
Output Value
Data Type in Java
Data types are different sizes and values that can be stored in the variable.
Types
There are two types of Data Types in Java
- Primitive data types
- Non-primitive data types
Primitive Data Types in JAVA
- Boolean data type
- Byte data type
- Char data type
- Short data type
- Int data type
- Long data type
- Float data type
- Double data type
If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.
To know more about JAVA Wikipedia please click here .
Stay Connected Stay Safe, Thank you
0 Comments