Variables in Java

Introduction

In Java, objects store their states in variables. Variables are used as containers to hold values (int, long, string…) during the life cycle of an application.

Variable Definition

To define a variable, we need to assign a data type for that variable. A data type defines the kind of value this variable can hold (int, long or String etc.).

Example of variable definition

	public final int var ;
public :  Access Modifier applied to variable
final    :  Non Access Modifier applied to this variable
int       :  Datatype. It defines kind of value this variable can hold (int in this case)
var      :   Name of the variable

Variable Initialization

Now that we are done defining a variable, we can initialize the above variable by assigning a value to it . In this case, we assign the variable an integer value.

	public final int var = 9;

Different Primitive Data Types

Java supports the below mentioned primitive data types.

  • byte
  • short
  • int
  • long
  • float
  • double
  • char
  • boolean

Note*: Corresponding Wrapper Classes are also available.

Variables Types in Java

Variables in Java can be defined anywhere in the code (inside a class, inside a method or as a method argument) and can have different modifiers. Depending on these conditions variables in Java can be divided into four categories.

  • Instance Variable
  • Static Variable
  • Local Variable
  • Method Parameter

Instance Variable(Non Static Fields)

Instance variables are used by objects to store their states. Variables which are defined without the STATIC keyword and are outside any method declaration are object-specific and are known as Instance Variables. Such variables are called instance variables because their values are instance specific and values of these variables are not shared among instances. For more details go to Instance Variable topic.

Class Variable(Static Fields)

 Variables which are declared with a STATIC keyword inside a Class (outside any Method) are known as Class variable / Static variable. They are known as Class level variables because values of these variables are not specific to any instance but are common to all instances of a class. Such variables will be shared by all instances of an Object. For more d

Local Variables(Method Local)

When a variable is declared inside a Method it is known as Method Local Variable. The scope of local variables is only inside the Method, which means local variables cannot be accessed outside that Method.  There are some restrictions on access modifier that can be applied to local variables. To know more about access modifier CLICK HERE. For more detail go to Local Variable article.

Parameters

Parameters are variables that are passed in Methods. For example, String args[] variables in the main Method is a parameter.

package com.jbt;

/*
 * Here we will discuss about different type of Variables available in Java
 */
public class VariablesInJava {

	public static void main(String args[]) {
		System.out.println("Hello");
	}
}

<< Java String Tutorial Local Variable in Java >>

93 Comments Variables in Java

  1. keerthi

    Hi Sir, very interesting I am new to programming, can you send the learning tree to place in the industry.

    Reply
  2. keerthi

    Hi Sir, very interesting I am new to programming, can you send the learning tree to place in the industry.

    Reply
  3. Shashikala

    when I click a button “My computer window should appear” can you please help me writing code for this
    Thanks in advance

    Reply
  4. Shashikala

    when I click a button “My computer window should appear” can you please help me writing code for this
    Thanks in advance

    Reply
  5. Manvendra V Singh

    Hi ,

    You have mentioned that- “Instance variable can only be accessed by Object of the class only as below.” in first example. But it is NOT correct. A method can use instance variable of it’s own class without using it’s object.

    For Example –
    class Employee {

    public Employee(String n, double s){
    name=n;
    salary=s;
    }

    public void print(){
    System.out.println(“Name = “+name+”Salary = “+salary);
    }
    private String name;
    private double salary;

    }

    Reply
    1. J Singh

      When we talk about accessing anything we talk about accessing it from outside of the class. Any method can access any variable irrespective of Access Modifier. There is no point in discussing that behavior. Hope it clarifies.

      Reply
  6. Manvendra V Singh

    Hi ,

    You have mentioned that- “Instance variable can only be accessed by Object of the class only as below.” in first example. But it is NOT correct. A method can use instance variable of it’s own class without using it’s object.

    For Example –
    class Employee {

    public Employee(String n, double s){
    name=n;
    salary=s;
    }

    public void print(){
    System.out.println(“Name = “+name+”Salary = “+salary);
    }
    private String name;
    private double salary;

    }

    Reply
    1. J Singh

      When we talk about accessing anything we talk about accessing it from outside of the class. Any method can access any variable irrespective of Access Modifier. There is no point in discussing that behavior. Hope it clarifies.

      Reply
  7. Mohammad Yakubpasha

    Hi Sir,
    This tutorial is very good, could you please share Total Java course in pdf format.
    Thank you

    Reply

Leave A Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.