Java variables is an important, fundamental and core java programming topic. Many FAQs in Java interviews are based on your knowledge of Java variables. These include questions on topics such as primitive variables vs reference variables, variables types, static vs non-static variables, access modifiers and non-access modifiers that can be applied to variables, scope of variables, transient variables, volatile variables, variables vs primitive data types etc.
A thorough understanding of variables is absolutely necessary for being successful in your your Java interview; specially for beginner to mid level positions; and for hands-on programmer, developer, architect or manager roles.
Below interview questions, answers, coding exercises and tips will help you get grounded in the fundamental concepts of Java variables. A solid foundation on variables will help you out in clearing the screening level as well as coding levels in later part of your interview process.
Important keywords are provided at the end of the questions. Review them, make them a part of Java vocabulary, and talk about them confidently during your interview process.
There are basically two different kinds of variables in Java programming language - Primitive variables and Reference variables. Primitive variables contain primitive literal values, where as reference variables contain a reference to an Object.
class MyClass {
//Primitive variable declaration - var1,
//var1 contains literal value 123.
int var1 = 123;
//Reference variable declaration - var2,
//var2 contains reference to object of type 'Box'.
Box var2 = new Box();
}
There are basically two different kinds of variables in Java programming language - Primitive variables and Reference variables. Primitive variables contain primitive literal values, where as reference variables contain a reference to an Object.
Based on scope, variables can be of four different types - Class variables, Instance variables, Local variables and Parameters. Scope of a variable is determined based on where it is declared within a java class.
1. Class variable (Static fields) - Class variables are variables declared within the class body, outside of any methods or blocks, and declared with 'static' keyword.
Class variables have the longest scope. They are created when the class is loaded, and remain in memory as long as the class remains loaded in JVM.
2. Instance variables (Non-static fields) - Instance variable are variables declared within the class body, outside of any method or block, and declared without 'static' keyword.
Instance variables have the second highest scope. Instance variables are created when a new class instance is created, and live until the instance is removed from memory.
3. Local Variables - Local variables are variables declared within a method body. They live only as long as the method in which it is declared remains on the stack.
4. Block variables - Block variables are variables declared within a block such as an init block or within a for loop. They live only during the execution of the block and are the shortest living variables.
class MyClass {
//Static variable
static String string1 = 'test string 1';
//Instance variable
String string2 = 'test string 2';
//Block variable in init block
{String string3 = 'test string 3'}
void perform() {
//Local variable
String string4 = 'test string 4'
//Block variable in for loop
for (int i=0; i < 4; i++) {...}
}
}
Static variables (or fields) are variables declared within the class body, outside of any methods or blocks, and declared with 'static' keyword.
Static variables have the longest scope. They are created when the class is loaded, and remain in memory as long as the class remains loaded in JVM.
Static variables are,essentially, global variables. A single copy of the static variable is created and shared among all objects at class level. All instances of the class share the same static variable.
A static variable can be accessed using the class, and without creating an object instance.
Class variables are stored on the heap.
class MyClass {
//Static variable
static String string1 = 'test string 1';
}
Instance variable are variables declared within the class body, outside of any method or block, and declared without 'static' keyword.
Instance variables have the second highest scope. Instance variables are created when a new class instance is created, and live until the instance is removed from memory.
Instance variables are stored on the heap.
class MyClass {
//instance variable
String string1 = 'test string 1';
}
Local variables are variables declared within a method body.
Local variables live only as long as the method in which it is declared remains on the stack.
Local variables are stored on the stack.
class MyClass {
void perform() {
//Local variable
Integer speed = 100;
}
}
Block variables are variables declared within a block such as an init block or within a for loop.
Block variables live only during the execution of the block and are the shortest living variables...
*** See complete answer and code snippet in the Java Interview Guide.
Final variables are variables declared with keyword 'final'. Once a value is assigned to a final variable it cannot be changed.
If final variable is a primitive variable, the primitive literal cannot be changed once it is assigned to the primitive variable...
*** See complete answer and code snippet in the Java Interview Guide.
Transient variable is a variable whose value is not serialized during serialization of the object. During de-serialization of the object, transient primitive variables are initialized to their default values...
*** See complete answer and code snippet in the Java Interview Guide.
Volatile variables are relevant in multi-threaded Java programming, in which multiple threads access the variables of an object. A volatile variable is declared with the keyword 'volatile'...
*** See complete answer and code snippet in the Java Interview Guide.