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

public 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();
    }
 }
Local Variable in JAVA

Let’s Run the code

Input Value

Output Value

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

public 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();
   }
}
Instance Variable in JAVA

Let’s Run the code

Input Value

Output Value

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

public 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);
    }
 }
Static Variables in JAVA Programming

Let’s Run the code

Input Value

Output Value

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

Categories: Software

Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *