Understanding Variables In Java

Subscribe to my newsletter and never miss my upcoming articles

Whether he is working, a student, or just getting into coding, every software developer will come across the term 'Variables' irrespective of the language he is learning.

In this blog, I will be talking about some variables and their usage in the Java language. With the explanations, the blog also has some code snippets to understand the concepts better. Even if you do not code in java, it will still be helpful since almost all variables, and their types remain the same across every language. Just the way we use them changes.


Variables are used to store data values in a code script.

  • In Java, a variable is always declared before being used.
  • When a variable is declared, a memory location is used, and the variable points to that memory name. All the changes made to the variable affect the memory location.
  • The value stores in the variable can be changed during program execution.

Consider the following example:

// Java Program to subtract two number

public static void main(String[] args){
     int num1=10;
     int num2=3;
     int ans=num1-num2;
     System.out.println("Difference  =" + ans);

Here, we used variables to store the values of two integers and their difference. Thus, a variable is a basic unit of storage in Java.

How to declare variables in Java?

The syntax to declare a variable in Java is very simple to write and understand. We can initialize the variable by specifying an equal sign and a value(Initialization is optional). However, the compiler never assigns a default value to an uninitialized local variable in Java.

Syntax to declare a variable: "type name"

type: Type of the data we wish to store in the variable. Example: Integer, String, Float.

name: Arbitrary name given to a variable

While writing variable names, we have to follow some rules; otherwise, the compiler will throw an error. The following are the rules:

  1. All variable names may contain uppercase and lower letters (a-z, A-Z), underscore ( _ ), dollar sign ($), and the digits 0 to 9. The dollar sign character is not intended for general use.
  2. No spaces and no other special characters are allowed.
  3. The name must not begin with a number.
  4. Java is case sensitive. Uppercase characters are distinct from lowercase characters.
  5. A Java keyword (a reserved word like library names, class names) cannot be used as variable names.

Types Of Variables

There are three types of variables-

  • Local Variables
  • Instance Variables
  • Static Variables

Let us dive into each of them in detail:

  • Local Variable: A variable defined within a block or method or constructor is called a local variable. The scope of these variables exists only within the block in which the variable is declared. i.e., we can access these variables only within that block. Initialization of the local variable is mandatory.

Consider the below example:

public class Human { 
    public void ManAge() 
        // local variable age 
        int age = 0; 
        age = age + 5; 
        System.out.println("Man age is : " + age); 

    public static void main(String args[]) 
        Human obj = new Human(); 
Man age is : 5

In the above program, age is a local variable to the function ManAge(). If the variable is used outside the ManAge() compiler will throw an error. Try it out on the compiler.

- Instance Variables: Instance variables are non-static variables and are declared in a class outside any method, constructor, or block. Initialization is not mandatory. The default value is zero. The instance Variable can be accessed only by creating objects.

Consider the following example:

import java.io.*; 
class Shopping { 
    // These variables are instance variables. 
    // These variables are in a class 
    // and are not inside any function 
    int Fruits; 
    int Bread;

class PriceList { 
    public static void main(String args[]) 
        // first object 
        Shopping obj1 = new Shopping(); 
        obj1.Fruits = 20; 
        obj1.Bread = 50; 

        // second object 
        Shopping obj2 = new Shopping(); 
        obj2.Fruits = 80; 
        obj2.Bread = 60;  

        // displaying marks for first object 
        System.out.println("Price for first object:"); 

        // displaying marks for second object 
        System.out.println("Price for second object:"); 
Price for first object:
Price for second object:

Fruits, Breads are instance variables. It is clear from the above output that each object will have its copy of the instance variable.

  • Static Variables: These variables are declared similarly as instance variables. The difference is that static variables are declared using the static keyword within a class outside any method constructor or block. Unlike instance variables, we can only have one copy of a static variable per class irrespective of how many objects we create.

Suppose we access the static variable like the Instance variable (through an object). In that case, the Compiler will show the warning message, and it will not halt the program. The Compiler will replace the object name with the class name automatically. If we access the static variable without the class name, Compiler will automatically append the class name.

Consider the following example:

import java.io.*; 
class Worker { 

    // static variable salary 
    public static double salary; 

public class Emp { 
    public static void main(String args[]) 

        // accessing static variable without object 
        Emp.salary = 1000; // giving salary variable a value

So, this was a little intro into understanding variables and how to use them in Java language. If you found it useful I have also written few more articles which will help you in Java Programming. Check them out!!

No Comments Yet