Chapter 4. Types of variables

Java programming language contains the following kinds of variables:

3.1. Local Variable

3.2. Instance Variable

3.3. Class Variable

3.4. Parameters

3.1. Local Variable

Variables that are declared in a method or constructor or block are called local variables. This type of variable can only be accessed locally in which they are declared.

Syntax to declare the local variable:

        Type identifier;

                where

                Type - Kind of data that the variable is going to contain

                Identifier - Name of the variable

                ; -  End of statement in Java

Imagine a paragraph without any period (.). The reader might be facing difficulties in understanding that paragraph. Likewise java compiler needs to know where each statement ends by finding the semicolon(;).

Example 1:

In the following example, the salary is the local variable. This variable is accessible within the getSalary( ) method only.

public void getSalary( )
{
    float salary;
    salary = wage * day;
}

Example 2:

The count variable is a local variable and it can be accessed only within the for loop.

for ( num=0; num < 10 ; num++)
{
    int count;
    system.out.println(count);
}

3.2. Instance Variable

Instance variables are unique for each instance of a class. An instance of a class is created each time, the system creates a copy of the instance variable related to that class. Example you have a class called “pen” with attributes “color” and “type”. Whenever you instantiate the “pen” class, these attributes are used separately in every object so you can create a “blue” pen and a “black” pen. In order to create, each object needs to have its own set of variables. And that set is called as instance variable.

Example 3:

Following example shows that how you can use instance variable:

public class Counting
{
    int count;
    Counting()
    {
        count++;
    }
}

public static void main(string[] arg)
{
    Counting c1 = new Counting();
    system.out.println(“C1 count is “ + c1.count );
    Counting c2 = new Counting();
    system.out.println(“C2 count is “ + c2.count);
    system.out.println(“C2 count is “ + c2.count);
}

The output of the above program as follows:

C1 count is 1
C2 count is 1
C2 count is 1

3.3. Class Variable

Class variable or static variable is a variable which is declared with static modifier in their declaration. This variable will have only one fixed location in memory and can be shared by every instance of the class. Class variables can be referenced by using the name of the class without creating the instance of the class.

Example 4

class Employee {
    private static int empNo=0;
}

Employee.empno;
system.out.println(Employee.empno);
Employee.empno++;
Employee.empno;
system.out.println(Employee.empno);
 

3.2. Parameters

Parameter is a variable which takes on the meaning of corresponding argument passed in a call to a method or constructor or exception handler. The scope of this variable is only within the method or constructor which is passed to. During the runtime, the value will be taken from the call of method or constructor and passed it to the parameter variable.

There are two kinds of data type you can use for parameters.

  1. Primitive data types (int, float, double etc.)

  2. Reference data types (objects or arrays)

Example 5:

public void calSalary (int days, float salperday)

{
    float salary;
    salary = days * salperday;
}


public static void main (string[] args)
{
    calSalary(20,150.00)
}

In the above example, the main method is calling the calSalary method and it is passing two values. calSalary method contains two parameters. The first parameter variable is days and the data type is integer and the second one is salperday with the data type of float. These two variables are used to calculate the salary and can only be used inside the calSalary method.