Variables in Java – Java Variable Declaration Rules

1. Java Variable – Objective

In our last tutorial, we discussed Java Data Types. Here, in this tutorial for variables in Java, we are going to learn what is variable in Java and different types of java variables that are used in the Java language, how to declare variables in java and how to use them.

So, let’s start Java Variables Tutorial.

Variables in Java

Java Variable Declaration Rules

2. What are Variables in Java?

Variables in Java are basically memory allocations, named storages which can later manipulate by the program. Each java variable has a specific type, which determines its memory and the data that can store in that variable. All variables should declare beforehand in Java.

a. Java Variable Declaration

There are 3 ways to declare variables:

  • Datatype – The Java Datatype that can store in that variable.
  • Variable_name – The name of the Java variable.
  • Value – The initial value stored in the variable.

Example-

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

How to Declare Variables in Java?

3. Types of Java Variables

There are three types of Java variables, they are

Types of variables in Java

Types of Java Variables

a. Local Variables in Java

Local variables are defined within a java method, block, or constructor and their scope remains limited to java blocks, classes in Java, and java constructors only.

These variables in Java creates when a function calls and delete once they exit the block or call return. Before discussing the example, let’s revise Java Operators and Java Access Modifiers.

Example-

public class StudentDetails
    {
       public void StudentAge()
        {  
//local variable age
            int age = 0;
            age = age + 5;
            System.out.println("Student age is : " + age);
        }
public static void main(String args[])
       { 
            StudentDetails obj = new StudentDetails();
            obj.StudentAge();
       }
  }

b. Instance Variables in Java

Java Instance variables declare in a class outside any method, constructor, or block, it is of static type. These Java variables create when an object created and destroyed once the object is destroyed. We can define the access specifiers and if nothing is mentioned that the default specifier is defined.

Example-

import java.io.*;
  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);
              }
  }

c. 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 being in java syntax that it uses the keyword static with it. It can also have as many copies which means it can use for many classes. They create at the start of program execution and destroy once 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)
    }
 }

So, this was all about Variables in Java. Hope you like our explanation.

4.  Conclusion

Hence, we studied the different types of variables and how to declare and uses of every variable type in java. Still, if you have any query, feel free to ask in the comment box.

See Also – How to Convert Colored Image in Java

For reference

Leave a Reply

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