Variables in Java Explained

  • Post author:
  • Post category:Java / Java Basic
  • Post last modified:July 7, 2021
  • Reading time:3 mins read

A variable is a term that refers to a value that can be change in future. When I put int i=10, for example, the variable name is I and the value is 10. int is a data type that indicates the type of the variable. I will discuss about datatype in upcoming article.

How to Declare a variable in Java

To declare a variable, you have to following the given syntax:

data_type variable_name = value;

In java, here value is optional because , you can declare the variable first and then later assign the value to it.

Such as:

int age;  // initialized but not declared

or you can declare and initialized at the same time like

int age = 24; // declared and initialized

More Examples on how to declare variable in java:

Declare character in java:

char c = 'a';

Declare integer in java:

int number = 100;

Declare floating point number in java:

float amount = 50.5;

Declare long value in java:

long value = 231211L;

Declare string value in java:

String name = "Java Tutorials";

Variables naming convention in java

So, let’s talk about how to declare variable name means variable naming convention. We have to follow the rules when we want to create the variables in java:

  • Can’t contain whitespace such as int my age = 24; its a wrong way to declare variable. It should be like int myage = 24;
  • Variable name can begin with special characters such as $ and _
  • Variable names are case sensitive
  • Can’t start with numbers like int 1num = 10; It’s a wrong way to declare but it might be like int num1 =10;

Types of Variables in Java

There are three types of variables in Java.
1) Local variable 2) Static (or class) variable 3) Instance variable

Local Variable

These variables are declared within the class’s method. They have a method-specific scope, which means you can’t change or access their values outside of the method.


public class Main {


    public static void main(String[] args){

        int number = 100; // declare variable
        System.out.println(number); // printing number variable

        int getNumber = showNumber(500); // calling showNumber() method
        System.out.println(getNumber); // printing value which is get by showNumber() method
    }

    // declare a method that return int value
    public static int showNumber(int number){
        int value = number;
        return value;
    }

}

Here, number, getNumber and value are local variables.

Instance variable

A variable declared inside the class is called instance variable. Value of instance variable are instance specific.

An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks. Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class. Access modifiers can be given to the instance variable.

let’s take an example:


public class Employee {

    private String name="Jone Doe";
    private int age=34;
    
    public static void main(String[] args){

        Employee e1 = new Employee();
        System.out.println(e1.name); // Output: Jone Doe
        System.out.println(e1.age); // Output: 34
    }
}

Here, name and age are instance variable of Employee class.

Static (or class) Variable

When instance variable are declare with static keyword is known as static variable. Static variables are also known as class variable because they are associated with the class and common for all the instances of class.



public class Employee {

    private static String name="Jone Doe";
    private static int age=34;

    public static void main(String[] args){

        Employee e1 = new Employee();
        System.out.println(e1.name);
        System.out.println(e1.age);

        name = "Updated name"; // can be used without creating object.
        System.out.println(name);
    }
}

Hope you got clear idea about java variables.