Instance Variables in Java Programming
OOPs in Java
In this lesson, we will understand what is Instance Variable in Java Programming and how to create them along with some examples.
What are Instance Variables in Java?
Instance Variables are declared inside a class and are used to store values in an object. Each object has its own copy of instance variables that are not shared between other objects.
Example of creating Instance Variables
class Data
{
// creating instance variables
public String name;
public int age;
}
public class Example
{
public static void main(String args[])
{
// Creating an object x of the class Data
Data x = new Data();
}
}
In the above example, we created two instance variables: name and age inside the class Data. After that, we created an object x of the class Data inside the main method.
Note: We have declared the instance variable using the keyword public, which means that we can access these variables from outside of its class using the object of the class.
Access and Modify Values of Instance Variables
We can access the value of an instance variable by using the object of the class followed by a dot (.) operator and then writing the name of the instance variable whose value we want to access.
We can modify the value of an instance variable by using the object of the class followed by a dot (.) operator and then writing the name of the instance variable with an equal sign, and then providing the new value to it.
Example
class Data
{
// creating instance variables
public String name;
public int age;
}
public class Example
{
public static void main(String args[])
{
// Creating an object x of the class data
Data x = new Data();
// Modify the values of instance variables
x.name="Peter";
x.age=15;
// Access the values of instance variables
System.out.println("Name: " + x.name);
System.out.println("Age: " + x.age);
}
}
Output
Name: Peter Age: 15
Suppose we declared the instance variables with the private keyword. In that case, the variables become private variables, meaning they can't be accessed from outside of their class using the object of the class.
Example
class Data
{
// creating instance variables
private String name;
private int age;
}
public class Example
{
public static void main(String args[])
{
// Creating an object x of the class data
Data x = new Data();
// Modify the values of private instance variables
x.name="Peter";
x.age=15;
}
}
Running the above program will give compilation errors.
error: name has private access in Data error: age has private access in Data
Initialized Instance Variables using Constructor
To initialize instance variables of a class, we use a method called Constructor.
A Constructor is a unique method whose name is the same as the name of the class inside which it is declared. Inside this method, we initialized the instance variables of the class.
There are two types of constructors and they are:
- Default Constructor
- Parameterized Constructor
Example
class Data
{
// creating instance variables
public String name;
public int age;
// Default Constructor
Data()
{
name="Thomas";
age=18;
}
// Parameterized Constrcutor
Data(String nm, int ag)
{
name=nm;
age=ag;
}
}
public class Example
{
public static void main(String args[])
{
// Creating an object x and y of the class Data
Data x = new Data();
Data y = new Data("William", 14);
// Access the values of instance variables of object x
System.out.println("Name: " + x.name);
System.out.println("Age: " + x.age);
// Access the values of instance variables of object y
System.out.println("Name: " + y.name);
System.out.println("Age: " + y.age);
}
}
Output
Name: Thomas Age: 18 Name: William Age: 14
Default Constructor does not take any parameter to initialize the instance variables. Parameterized Constructor takes arguments to initialize the instance variables during object creation in the main function.
In the above example, when declaring the object y, we pass values to its parametrized constructor to initialize the instance variables of the object y.
Use of this Keyword
We use this keyword to refer to the current object of the class. It is used to differentiate between the instance variable and the local variable having the same name.
Example
class Data
{
// creating instance variables
public String name;
public int age;
// Default Constructor
Data()
{
name="Thomas";
age=18;
}
// Parameterized Constrcutor
Data(String name, int age)
{
this.name=name;
this.age=age;
}
}
public class Example
{
public static void main(String args[])
{
// Creating an object x and y of the class Data
Data x = new Data();
Data y = new Data("William", 14);
// Access the values of instance variables of object x
System.out.println("Name: " + x.name);
System.out.println("Age: " + x.age);
// Access the values of instance variables of object y
System.out.println("Name: " + y.name);
System.out.println("Age: " + y.age);
}
}
Output
Name: Thomas Age: 18 Name: William Age: 14
In the above example, the instance variables' names and the formal arguments' names of the parameterized constructor are the same. We have used this keyword to distinguish between variables.