Home >>Java Tutorial >Java Variables

Java Variables

Java Variables

A Java variable is a container that is basically used to hold the value while the java program is being executed. Please note that a variable will be assigned with a datatype. Variable is a name of memory location. There are generally three types of the variables in java that are local, instance and static and there are generally two types of data types in java that are known as primitive and non-primitive.

Java Variable Identifiers

There is a need to identify all the Java variables with unique names and these unique names are known as identifiers. Identifiers can have short or long names and the short names will be like x and y or more descriptive names like age, sum, totalVolume.

Please note: It is generally recommended to use the descriptive names just to create the code that is understandable and maintainable.

Here are some of the general rules that are used for constructing the names for variables (unique identifiers) depicted below:

  • The variable's name can contain letters, digits, underscores, and dollar signs
  • The variable's name must begin with a letter
  • The variable's name should start with a lowercase letter and whitespace is not allowed.
  • The variable's name can also begin with $ and _
  • The variable's name are known to be case sensitive ("myVar" and "myvar" are different variables)
  • The variable's name cannot have the reserved words as their names.

Syntax

type variable = value;

Here are some of the examples:

1. In this example we will create a variable called name of type String and assign it the value

String name = "Abhi";
System.out.println(name);

2. In this example we will create a variable called myNum of type int and assign it the value 15:

int Num = 10;
System.out.println(Num);

3. In this example we will change the value of myNum from 15 to 20:

int Num = 10;
Num = 20; //Now Num value is  20
System.out.println(Num);

Declaring Variables

In order to declare a variable, specifying the type and assigning it a value is a must:

Syntax

Here is the syntax of the variable in Java:

type variable = value;

In the above mentioned syntax, type is known as one of the Java's types (like int or String), and variable is known as the name of the variable (like x or name). In order to assign values to the variable, the equal sign is used.

Types of Variables

There are generally three types of variables in java that are depicted below:

  • local variable
  • instance variable
  • static variable

1. Local Variable

A local variable in java is a variable that is generally declared inside the body of the method. The programmer can use that variable only within that particular method and the other methods that are present in the class aren't even aware that the same variable even exists. It is also important to remember that a local variable cannot be defined with the "static" keyword.

Here is an example of local variable:

public class HelloApp
{
    public static void main(String[] args)
    {
        String str;
        str = "Hello World!";
        System.out.println(str);
    }
}

2. Instance Variable

An Instance variable is a variable that is basically declared inside the class but outside the body of the method. This variable is not declared as static. The value of the instance variable is instance specific and is not shared among instances hence; it has the name instance variable.

Here is an example of the Instance variable:

class Page 
{
public String pageName;
// instance variable with public access
private int pageNumber;
// instance variable with private access
}

3. Static variable

A static variable is basically a variable that is declared as static and it isn't allowed to be local. The programmers can create a single copy of static variable and then can share that among all the instances of the class that are present. When the class is loaded in the memory then only the memory allocation for static variable happens.

Here is an example of the static variable:

class Demo 
{ 
    // static variable 
    static int x =test(); 
  
    // static block 
    static
    { 
       System.out.println("This is Inside static block"); 
    } 
  
    // static method 
    static int test() 
    { 
        System.out.println("Print from test"); 
        return 10; 
    } 
  
    public static void main(String[] args) 
    { 
        System.out.println("Print the Value of x : " + x); 
    } 
}

Example Sum of Two numbers

class Demo
{  
	public static void main(String[] args)
	{  
	int x=10;  
	int y=10;  
	int z=x+y;  
	System.out.println(z);  
	}
}