Understanding how to declare and use variables is fundamental to writing effective Java code.
In this tutorial, we will explore the concept of variables in Java. Variables are essential components in any programming language, as they allow us to store and manipulate data during program execution.
In this guide, we will cover the different types of variables in Java, including local variables, instance variables, and static variables.
We will also learn about variable declaration and initialization, and the key differences between these types.
So, let's dive into the fascinating world of Java variables and discover how they play a crucial role in developing robust and dynamic applications.
What is a variable
A variable is a value that can change, depending on condition or on information passed to the program.
A program consists of instructions that tell the computer what to do with the data that the program uses when it’s running. The data consists of constants (fixed values that never changes) and variables.
Both constants and variables are defined as certain data types. Each data type determines and limits the form of the data.
In Java, we have 3 types of variables:
- Local variables
- Instance variables
- Static variables
Before seeing how to declare different types of variable, it’s important to know the difference between variable declaration and variable initialization.
This is variable declaration → String name
;
Variable declaration needs 2 components:
- data type : Type of data that can be stored in this variable
- data name : Name was given to the variable
This is variable initialization → String name = “Kevin”
;
Variable initialization needs 3 components:
- data type : Type of data that can be stored in this variable
- data name : Name was given to the variable
- value : The initial value stored in the variable
What it looks like in practice
// Declaring float variable
float simpleInterest;
// Declaring and initializing integer variable
int distance = 10;
How to declare a local variable
A variable defined within a block or method or constructor is called a local variable. The scope of these variables exists only within the block in which they are declared, that means we can access these variables only within that block.
//...
public void showAge() {
// Declaring a local variable
int age = 25;
// This variable is local to the showAge function only
System.out.println(age);
}
How to declare an instance variable
Instance variables are non-static variables and declared outside any method, constructor or block. Initialization of an instance variable is not mandatory.
As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed. Its default value is dependent on the data type of variable.
For String, it is null
, for float it is 0.0f
, for int it is 0
, for Wrapper classes like Integer it is null
, etc.
Instance variables can be accessed only by creating objects.
We initialize instance variables using constructors while creating an object. We can also use instance blocks to initialize the instance variables.
class Person {
// Declaring instance variable
String name;
String email;
public Person() {
// Initializing instance variable
this.name = "Kevin";
}
}
Instance blocks can be used to initialize instance variables. Java also provides the ability to initialize fields during object creation using instance initializer blocks, such blocks serve the same purpose as constructors during object creation.
The code in the local block (1) is executed every time an instance of the class is created.
class School {
public String address;
{ // (1) local block
address = "14 rue du maréchal viscount, 75000 Paris";
}
public String getAddress() {
return this.address;
}
}
If you instantiate a new School object, you will get this result
//...
var school = new School();
System.out.println(school.getAddress()); // 14 rue du maréchal viscount, 75000 Paris
//...
How to declare a static variable
Static variables are also known as class variables. They are declared using the static keyword within a class outside any method, constructor or block. They are created at the start of program execution and destroyed automatically when execution ends.
Initializing a static variable is not mandatory. Its default value is dependent on the data type of variable. For String it’s null
, for float it is 0.0f
, for int it is 0
, for wrapper classes like Integer it is null
, etc.
class Car {
// Declaring static variable
public static String model;
}
Static blocks can be used to initiate static variables:
class Car {
// Declaring static variable
public static String model;
static { // (1) static block
System.out.println("This is a car");
}
}
Differences between Instance and Static Variables
- We can access instance variables through object references, and static variables can be accessed directly using the class name.
- Changes made in an instance variable using one object will not be reflected in other objects, as each object has its own copy of the instance variable. In the case of a static variable, changes will be reflected in other objects, as static variables are common to all objects of a class.
- Instance variables are created when an object is created with the use of the keyword ‘new’ and destroyed when the object is destroyed. Static variables are created when the program starts and destroyed when the program stops.
I hope you enjoyed reading this, and I'm curious to hear if this tutorial helped you. Please let me know your thoughts below in the comments. Don't forget to subscribe to my newsletter to avoid missing my upcoming blog posts.
You can also find me here LinkedIn • Twitter • GitHub or Medium
Ending
They are some key takeaways from the article :
- Variables in Java is a data container that saves the data values during Java program execution
- There are 3 types of variables in Java — Local, Static, Instance
- They are few difference between Instance and Static Variables