Types of Variables
There are totally three types of variables that you can declare in your program:
- Instance Variables - these belong to the specific instance and every instance has its own copy of the variable. You already worked with this in the earlier lesson
- Local Variables - these belong to the specific code block like methods, constructors, if and while blocks etc.. You have already used these as well in all your programs
- Static Variables - these belong to the Class as a whole and not to any specific instance. There is only one copy of the static variable in memory. You should be careful when declaring a variable static as the same copy is referenced by all instances.
static variables
When you declare a variable static in a class, then you only have one copy of that variable in memory for any number of objects that might be created with that class. This is in contrast to the instance variables, in which, there is an individual copy of that variable for every single object that was created. Let us take an example:
class Student {
static int numberOfStudents;
private String name;
public String getFirstName() {
return this.name;
}
public void setFirstName(String name) {
this.name= name;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public Student() {
numberOfStudents++;
}
public static void main(String[] args) {
Student student1 = new Student();
student1.setFirstName("John");
System.out.println(student1.getNumberOfStudents());
Student student2 = new Student();
student2.setFirstName("Jane");
System.out.println(student2.getNumberOfStudents());
}
}
In the above example, you see that the variable firstName is an instance variable as it does not contain static modifier. However variable numberOfStudents is not an instance variable as it contains the keyword static as its modifier. It makes sense to make the firstName an instance variable, as every object (instance) has to keep its own value to distinguish individual students.
In this example you also see a no argument constructor for Student class, which increments the value of numberOfStudents. So what is going on here? When ever a Student object is instantiated with the new keyword, the value of the static variable, numberOfStudents is incremented by 1. While every instance should keep a separate value of its own to distinguish the value of name across instances, you do not need to keep a separate value for every instance when it comes to tracking the number of student objects created. You only need one value across all the instances. When you have scenarios of this sort, you declare a static variable. A static variable is also called a class variable as it belongs to the class and not to any instance although every instance can access the class variable.
Another Example
Now let us take another example of instance and static variables. The below picture depicts code in which two instances of Customer are created and the name of the Customer object is initialized with values "John" and "Jane". This class also has a static variable customerCount. Notice you have two copies of name variable; one each for each of the objects instantiated and you only have one copy of the static variable which belongs to the class. However both the object instances can access the static variable and make changes to its value. Even though the instances can access a static variable though the instance identifier, it is recommended to use the class name instead. Although in this example we are accessing the static variable using the instance to help understand the concept of static variables, it is recommended to access static variables though the class name and hence both customer1.customerCount++ and customer2.customerCount++ should be replaced with Customer.customerCount++.
Points to remember!
- Since static variables are kept in one single memory location, any instance can overwrite the static value set by another instance.
- Although you can reference a static variable through an instance, creating an instance is not required and a static variable should be accessed through the Class name.
Variable Scope
Local Variable Scope
A local variable can be accessed only by the statements which are after its declaration and are within the code block that the local variable belongs to.
class Lion {
static int totalLions;
public void speak() {
String greet = null;
if (age < 10) {
int i =0;
System.out.println(i);
greet = "Roar...";
} else {
greet = "Whine...";
}
System.out.println(greet);
}
public Lion (int age) {
totalLions++;
this.age = age;
}
private int age;
}
The local variable greet in the above code block cannot be accessed by the constructor.
Static and Instance Variable Scope
A static or instance variable can be accessed by any code block within the class and even outside the class if not declared as private. The instance and static variables can be declared in any part of the class although it is a good practice to declare all variables on the top, just below the class declaration.