In Java, variable is a name given to a memory location and this variable is associated with a value.

int x = 99;

int – data type
x – variable
99 – value

variable x holds integer values and its current value is 99.

Let’s see how to declare variables in Java

Syntax to declare a variable in Java:

data_type variable = value;

Example:

int x = 99;

Variable Naming Convention in Java:

Earlier we have learnt that Java is a Case Sensitive Language. Even variables have their own naming convention to follow.

1. Variable name can starts with special characters such as _ or $

Example:

int $myAge;

2. Variable name should begin with lower case leter

Example:

Wrong way: int Age;
Correct way: int age;

3. If the variable name consists of more than one word, it’s a best practice to capitalize the first letter of each subsequent word.

Example:

Wrong way: int myage;
Correct way: int myAge;

4. Variable name should not contain white spaces

Example:

Wrong way: int my Age;
Correct way: int myAge;

Types of Variables in Java:

There are three types of variables in Java.

1. Local variable
2. Instance variable
3. Class/Static variable

Let’s see each variable in detail.

Local Variable:

Local variable is a variable which we declare inside a Method. A method will often store its temporary state in local variables.

Instance Variable (Non-static):

Instance variable is a variable which is declared inside a Class but outside a Method. We don’t declare this variable as Static because these variables are non-static variables.

Class Variable (Static):

Class variable is a variable which is declared as Static. Additionally, the keyword final could be added to include that the value will never change.

Sample Program 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package classTwoVariables;
 
public class VariablesLocalInstanceClass2 {
static int staticVar = 100; // static variable
int instanceVar = 200; // instance variable
public static void main(String [] args){
int localVar = 300; // local variable
// We can access static variables without creating an Object of a class
System.out.println(“Value of a Static Variable is “+staticVar);
// Creating an instance of a class ‘VariablesLocalInstanceClass2’
VariablesLocalInstanceClass2 var = new VariablesLocalInstanceClass2();
System.out.println(“Value of a Instance Variable is “+var.instanceVar);
System.out.println(“Value of a Local Variable is “+localVar);
}
 
}

Sample Program 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package classTwoVariables;
 
public class VariablesLocalInstanceClass {
final static int staticVar = 100; // static variable
int instanceVar = 200; // instance variable
public static void main(String [] args){
int localVar = 300; // local variable
// We can access static variables without creating an Object of a class
System.out.println(“Value of a Static Variable is “+staticVar);
// We cannot access instance variables without creating an Object of a class
//System.out.println(“Value of a Instance Variable is “+instanceVar);
// Local variables are limited to this method only
System.out.println(“Value of a Local Variable is “+localVar);
// Creating an object of VariablesLocalInstanceClass
VariablesLocalInstanceClass var = new VariablesLocalInstanceClass();
//Updating the value of localVar
System.out.println(“Value of a Instance Variable is “+var.instanceVar);
localVar = 301;
System.out.println(“Updated value of a Local Variable is “+localVar);
//Updating the value of instanceVar using the object of VariableLocalInstanceClass
var.instanceVar = 201;
System.out.println(“Updated value of a Instance Variable is “+var.instanceVar);
/*We can modify the static variable by creating an instance of a class
but to avoid this we can set final keyword
I have added final keyword to the static variable…
If you uncomment the below two statements, you can see an error
If you remove final keyword of static variable staticVar then you wont face any error*/
/*var.staticVar = 101;
System.out.println(“Updated value of a Static Variable is “+staticVar);*/
}
 
}

Must Read: Java Tutorial