Variables and object references
A variable, is conceptually a container, where we keep data. Java is a strongly typed language. Variables are always defined with an associated type. When you store data, the type of data and the type of variable have to be the same.
Declaration of variables
To define a variable in Java, just write a type, followed by the name you want to give the variable.
int i; Date date;
In the example we define the variable i as an integer primitive type; int and date as a Date type. The semicolon is a statement separator in Java.
We can also see that Java is case sensitive. Java Date, date or dAte are completely different identifiers with no relationship between them.
i = 5; date = new Date();
Here is how we can assign or store data into variables.
In the second line, we see how we create and save a Date type object in the date variable. Actually, what stores the variable date, is not the object, but a reference to the object.
Object References
A variable is a location in memory, where data is stored. This place in the memory is called Stack.
In the case of primitive data, as in "int i = 5", there are four bytes in the Stack where number 5 is stored.
When an object is created in Java, as in "new Date ()", the object is stored in a memory location called Heap. When assigning the object to a variable as in "date = new Date ()", we store the Heap memory address where the object is, in date.
Let´s consider the implications of this indirect referencing, with a code example:
int i1 = 5; int i2 = i1; Date date1 = new Date(); Date date2 = date1;
In the graphic below, we can see how the memory stores the date, after running these assignments. The stack stores the 4 variables. i1 and i2 have independent copies of the value of 5. Unlike the case of date1 and date2, where these variables store a pointer to the same object.
The important thing here, is to understand that any change in the Date object will be reflected in both date1 as in date2. This behavior often brings confusion and mistakes. This problem does not exist in the case of i1 and i2, if we modify i1 ;"i1 = 3", i2 will still be 5.
Default initialization and null value
When you declare a variable, but you don´t initialize it, Java does it for you. In the case of primitive types, Java chooses a default value. In the case of numeric and int types it initializes to 0.
In the case of objects, Java initializes it with a value or reference called null. If you write; "Date date = null;" it is the same as writing; "Date date;"
Instance variable or object member field
An instance variable or field is a variable associated with an object. It is said to be a variable or field, a member of the object. It is declared in the body of the class.
Each time a new instance of an object is created, a new variable associated with the object is created. There is one for each object created and if you have not created an object, it is not accessible.
If you, for example, create a class Person, it could have the fields; name, surname and address.
class Person{ String name; String surname; String address; ... }
Below, we can see, how to access the fields in a Person object:
Person person = new Person(); person.name = "Eduardo"; person.surname = "Medina"; person.address = "Gran Via 27";
Static variable
A static variable is associated with a class rather than with an object. To access it, simply write the name of the class followed by a dot and the name of the static variable. You don´t have to create any object.
It is declared in the body of the class, the same as the field, but preceded by the word "static".
For example, in the Person class, we could define a totalNumberPeople static variable, to count how many objects Person we have. To declare this static variable we would write:
static int totalNumberPeople;
To use it, we would write:
Person.totalNumberPeople = 5;