Learn 3 Types of Variables in Java with Examples

Do you ever wonder where the value of an element is stored? The answer is variables. Variables in Java help to store the values of elements for further operations. There are 3 types of Java variables which we will cover in this article with examples.

By the end of this article, you will be well experienced with the concept of variables and can start working on it.

Before we start discussing Variable, you should know Data Types in Java.

What are Variables in Java?

Java Variables are basically memory allocations, It is the place where we store values which can later be manipulated by the program. Each variable has a specific type, which determines its memory and the data that can store in that variable. All variables should be declared beforehand in Java.

Java Variable Declaration

There are 3 ways to declare variables-

  • Datatype – The Datatype is the type of data in which the variable is stored.
  • Variable_name – It is the name of the variable.
  • Value – The initial value stored in the variable.

Declaration of Variables in Java

Example-

float simpleInterest; //Declaring float variable
int time = 10, speed = 20; //Declaring and Initializing integer variable
char var = 'h'; // Declaring and Initializing character variable

Types of Variables in Java

Types of Variables in Java

1. Local Variable in Java

A local variable is defined within a method, block, or constructor, and its scope remains limited to a particular block. Its lifetime is within the parenthesis in which it is declared.

We can also use local variables with the same name in different functions. In this case, the name of the variable is the same but they act differently it is similar like two persons having the same name.

Do you know how to define and declare methods in Java?

Example-

package JavaVariablesDemo;
public class CourseDetails {
  public void NumberOfCourses()
   {

           int course = 10;//declaring local variable course
           System.out.println("Number of courses at DataFlair are : " + course);
       }
public static void main(String args[])
{ 
    CourseDetails obj = new CourseDetails();
    obj.NumberOfCourses();
}
}

Output-

Screenshot of output

2. Instance Variables in Java

Instance variables are declared in a class, outside any method, constructor, or block, it is of the static type. These variables are created when an object is created with the help of keyword new and destroyed when the object is destroyed. Instance variables have by default some values, for numbers, the value is 0, for Booleans it is false and for the object references the value is null.

Recommended Reading – Constructor in Java with Example

Example-

  class Marks
     {
//These variables are instance variables.
//These variables are in a class and are not inside any function
          int engMarks;
          int mathsMarks;
          int phyMarks;
     }
class MarksDemo
     {
          public static void main(String args[])
             { 
//first object
                    Marks obj1 = new Marks();
                    obj1.engMarks = 50;
                    obj1.mathsMarks = 80;
                    obj1.phyMarks = 90;
//second object
                    Marks obj2 = new Marks();
                    obj2.engMarks = 80;
                    obj2.mathsMarks = 60;
                    obj2.phyMarks = 85;
//displaying marks for first object
                    System.out.println("Marks for first object:");
                    System.out.println(obj1.engMarks);
                    System.out.println(obj1.mathsMarks);
                    System.out.println(obj1.phyMarks);
//displaying marks for second object
                    System.out.println("Marks for second object:");
                    System.out.println(obj2.engMarks);
                    System.out.println(obj2.mathsMarks);
                    System.out.println(obj2.phyMarks);
              }
  }

Output-

Screenshot

3. Static Variables in Java

We declare static keyword in Java same as the instance variables, i.e. outside any object, block, or constructor, the difference in the syntax is that it uses the keyword static with it. It can also have as many copies which means it can be used for many classes. They are initialized at the beginning of program execution and destroyed when the program ends.

Example-

import java.io.*;
      class Emp {
// static variable salary
          public static double salary;
          public static String name = "Harsh";
       }
public class EmpDemo
   {
       public static void main(String args[]) {
//accessing static variable without object
       Emp.salary = 1000;
       System.out.println(Emp.name + "'s average salary:" + Emp.salary)
    }
 }

Output-Screenshot

Summary

Variables in Java are an essential part of programming. It helps the programmer to write flexible programs. Now, you can declare 3 types of variables in the program. Every variable is nicely explained with examples. If you find any difficulties, please share with us!

Don’t go, it’s time to explore the concept of Operators in Java

2 Responses

  1. Dorice Kagisa says:

    I really like how you guys explain and try to put some concepts clear
    Have just started to learn java …..wanna be more …..and I hope this is the perfect place where I can get help with all the questions I have

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.