Types of Variables in Java with Example

The variable is the basic unit of storage in a Java program. A Variable is the name of a memory location. The value of the variable can be changed during the program execution. In Java, there are 3 types of variables: Instance variables, Static variables, and Local variables. All these variables have a scope, which defines their visibility, and a lifetime.


How to declare a variable in Java?

In java, all the variables must be declared before they can be used. The syntax of Java variable declaration is shown here:
Syntax: data_type variable_name = value;
for example:
int a = 20;
Here int is a data type, a is the variable name, and 20 is the value assigned to the variable a.

If you want to declare more than one variable of the specified type, use a comma-separated list. for example:
 int a = 10, b = 20, c = 30; // declare & initialize 3 int variables 


Java Variable Naming Convention:

1. Variable names are case sensitive which implies that the variable name in lowercase is different from the variable name in uppercase.
2. Variable names only start with a letter, an underscore(_) or a dollar sign($).
3. Variable names cannot start with any number.
4. Variable names cannot contain any white spaces. for example:
 int num ber = 10; //invalid because the variable name has space in it. 



Types of variables in Java:

There are three types of variables in Java.
1. Local Variable.
2. Instance Variable.
3. Static Variable


1. Local Variable in Java:

Local Variables are declared and used inside the method or block or constructors. The scope of these variables are exists only within that block in which the variable is declared


Example of Local Variable:


public class EmployeeDetails{
public void employeeAge(){
int age = 30;
age = age + 10;
System.out.println("Employee age is: " +age);
}
public static void main(String args[]){
EmployeeDetails obj = new EmployeeDetails();
obj.employeeAge();
}
}


Output:
Types of Variables in Java

In the above example, the variable age is the local variable to the function
employeeAge(). If we use the variable age outside the employeeAge() function
then the compiler will give a compile-time error as shown below in the program.

public class EmployeeDetails{
public void employeeAge(){
int age = 30;
age = age + 10;
}
public static void main(String args[]){
System.out.println("Employee age is: " +age);
}
}


Output: Shows compile-time error.
Types of Variables in Java

2. Instance Variable in java:

An instance variable is a variable that declared inside the class but outside the body of the method. This type of variables are created when an object of the class is created and destroyed when the object of the class is destroyed. The value of the instance variable is instance specific and cannot be shared among all the instances.



Example of Instance variable:


class EmployeeDetails{
String name; // instance variable
int age; // instance variable
String address; //instance variable
}
class EmployeeDemo{
	public static void main(String args[]){
		
		//first object
		EmployeeDetails obj1 = new EmployeeDetails();
		obj1.name = "Ramesh";
		obj1.age = 30;
		obj1.address = "Delhi";
		
		//Second object
		EmployeeDetails obj2 = new EmployeeDetails();
		obj2.name = "Prashant";
		obj2.age = 20;
		obj2.address = "Chandigarh";
		
		//dispalying employee details for first object
		System.out.println("Employee details for first object: ");
		System.out.println(obj1.name);
		System.out.println(obj1.age);
		System.out.println(obj1.address);
		
		//dispalying employee details for second object
		System.out.println("Employee details for second object: ");
		System.out.println(obj2.name);
		System.out.println(obj2.age);
		System.out.println(obj2.address);
	}
}


Output:
Types of Variables in Java


3. Static variable in Java:

The variable which is declared static with the help of a static keyword is called the static variable in java. We can access the static variable without creating an object of the class. The static variable have only one copy per class and shared among all the instances of that class. We can access the static variable with the class name.

Example of Static Variable:


class EmployeeDetails{
	public static String name = "Prashant Srivastava";
	public static int age;
}
class EmployeeDemo{
	public static void main(String args[]){
		
		//accessing static variable without creating object.
		EmployeeDetails.age  = 20;
		System.out.println(EmployeeDetails.name +" age is: "+EmployeeDetails.age);
	}
}


Output:
Types of Variables in Java



You may also read these posts:

1. Java Introduction - JDK, JRE, JVM.
2. Java Hello World Program
3. How to set path in java?
4. Top 100 java interview question and answers.
5. Class & Objects in Java with example.
6. Java Complete collection framework tutorial.

Types of Variables in Java with Example Types of Variables in Java with Example Reviewed by Prashant Srivastava on December 12, 2019 Rating: 5

No comments:

Powered by Blogger.