Java Variables

Variable is a data name that may be used to store a data value. Variables are created when we assign a value to it.

Example:

int a, b, c;

Here a, b and c is the variable name which hold’s values from integer data type.

Conditions for a variable name:
  • A variable names can be a letter, digits and underscore character.
  • It must begin with a letter.
  • White space is not allowed in a variable name.
  • It should not be a keyword.

Valid Variable name:

int count;
float neural_beast;

Invalid Variable name:

float char;
int null group;
String word$;

Assigning values to a variable

(=) operator is used to assign a value to a variable. Eg: (int a = 10;), here 10 is the value assigned to the variable a.

Example to add two values:

class add_num
{
public static void main(String args[])
{
int a, b, c;
a = 10;
b = 20;
c = a + b;
System.out.println("The Sum of the value is:" + c);
}
}

Output:

The Sum of the value is:30

Types of Variables

There are three types of variable, they are.

  • Local Variable
  • Instance Variable
  • Static Variable

Local Variable

A variable declared and defined inside the body of the method or with in a block is called local variable. The range of the local variables exist only within the block. These variables can be accessed only within the block.

Example:

class employee_dt
{
public void employee()
{
int n, num = 10;
n = num + 1;
System.out.println(n);
}
public static void main(String args[])
{
employee_dt emp = new employee_dt();
emp.employee();
}
}

Output:

11

Instance Variables

Instance Variables are non-static variables which are declared inside the class but outside the body of method or a block. These variables are created when an object of class is declared and will destroyed when the object is destroyed. The default value of instance variable is 0. We can access instance variable by creating objects.

Example:

class employee
{
int employee_1;
int employee_2;
int employee_3;
int employee_4;
}
class employee_dt
{
public static void main(String args[])
{
employee emp1 = new employee();
emp1.employee_1 = 1;
emp1.employee_2 = 2;

employee emp2 = new employee();
emp2.employee_3 = 3;
emp2.employee_4 = 4;

System.out.println("The First Employee number is:" + emp1.employee_1);
System.out.println("The Second Employee number is:" + emp1.employee_2);
System.out.println("The Third Employee number is:" + emp2.employee_3);
System.out.println("The Forth Employee number is:" + emp2.employee_4);
}
}

Output:

The First Employee number is:1
The Second Employee number is:2
The Third Employee number is:3
The Forth Employee number is:4

Static Variables

Static Variables are declared using static keyword. To access static variable we want to append the class name with the variable, if we access static variable without the class name the compiler will automatically append the class name.

Example:

class employee
{
public static int employee_1 = 1;
public static String name;
}
class employee_dt
{
public static void main(String args[])
{
employee.name = "Regu";

System.out.println("The Employee number is:" + employee.employee_1 + "\t Employee name is:" + employee.name);
}
}

Output:

The Employee number is:1         Employee name is:Regu

More Reading

Post navigation

8 Comments

Leave a Reply

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