Java supports different variables to store data in memory. There are two major types of variable in java, known as primitives and complex objects. Primitives variables are stored in fast as possible in the memory. They are numeric values, single character and boolean values everything else we use to store in memory in java application will be represented as a complex object. We declare a complex object as an instance of a java class eg. strings, date values and everything else other than primitives.


Primitive variables are declared in three parts i.e. Data type, variable name, initial value of the variable (optional).

int newVariable = 10;

int is the data type of the variable, newVariable is the name of the variable. Name of the variable should always start with a small letter or underscore (_) but should not start with a number. The last part of the declaration is optional, we use the equal operator which is assignment operator and we pass the value that is compatible with the datatype. If we don’t initialize any value to the variable it will have a default value i.e. 0 for integer and false for boolean.

Now lets declare a complex object, Declaration of a complex object will be instance of class, they are also declared in three parts, the data type, name and initialized with some value which is compatible with its data type. As in primitive data type third part is optional in complex data type too. We use the new keyword and a particular kind of function or method called a constructor method of the class. The name of the constructor method matches the name of the class, Ex:

Date newDate = new Date();

If we see carefully, we will notice that complex data type always starts with a capital letter, this is because they are class and classes always starts with a capital letter by java conventions. We use new keyword because we created an instance of the class. For complex object if we eliminate the assignment it will still exist but it will not point anything in memory and we say that variable has a value of null. When we declare the variable we should know the scope of the variable. For example, if we declare the variable inside the function we say it is local to the function and when the function is finished executing the variable is dereferenced.

code 1:

void localVariable(){
String variableExample = new String("This is local variable");
System.out.println(variableExample);
}

code 2:

void localVariable(){
String variableExample = new String("This is local variable");
}
System.out.println(variableExample);

Code 1 will work fine as we have declared a string localVariable in the function and we are trying to reference it within the function but code 2 won’t as we are trying to reference the localVariable outside the function. To fix this problem, we need to make it class variable also know as fields in java. Here is an example:

public class ClassExample {
       String variableExample = new String("This is local variable");
       void localVariable(){
            System.out.println(variableExample);
       }
}