This write-up aims to present a detailed understanding of the Java instance variables:
- What does an instance variable mean in Java?
- Instance Vs. local variables
- Practical Implementation
What does an instance variable mean in Java?
An instance variable is declared/defined at the class level but outside the methods, blocks or constructors. The instance variables are accessible within any method, block, or constructor of the same class. It is created with object creation and destroyed with object destruction.
The below-given table will present more details about the instance variables in Java:
Feature | Description |
---|---|
Access modifier | Can be declared using the default, public, private, or protected modifiers. |
Visibility | Visible to blocks, constructors, and methods within the class. |
Default Value | 0 for numeric types like int, float, short, etc. It is false for Booleans. Null for object references. |
Accessibility | Within the same class, instance variables are accessible using the variable’s name. |
Instance Vs. local variables
The below-given table will present a comparative analysis between the instance variables and local variables:
Metrics | Instance Variables | Local Variables |
---|---|---|
Declaration | Declared at class level but outside method/block/constructor. | Declared inside a method. |
Variable creation | It is created with object creation. | Created at the time of a method call. |
Default Values | Have some default values. | Doesn’t have default values. |
Scope | Accessible anywhere in the class. | Accessible within a method only. |
Usage | Stores the values needed to be accessed by various class methods. | Stores the values needed to be accessed by a specific class method. |
Practical Implementation
The below-given program will show you how to declare and access instance variables in Java:
package exampleclass;
public String empName;
String emailAddress;
private int empId;
protected int empAge;
ExampleClass(String eName, String eAddress)
{
empName = eName;
emailAddress = eAddress;
}
public void setEmpData(int eId, int eAge)
{
empId = eId;
empAge = eAge;
}
public void showDetails()
{
System.out.println("Employee Name: " + empName);
System.out.println("Employee Email Address: " + emailAddress);
System.out.println("Employee Id: " + empId);
System.out.println("Employee Age: " + empAge);
}
public static void main(String args[])
{
ExampleClass exObj = new ExampleClass("John", "[email protected]");
exObj.setEmpData(13, 25);
exObj.showDetails();
}
}
In the above-given code block, we performed the below-listed functionalities:
- Firstly, we created four instance variables with four different access modifiers.
- Next, we created a constructor to initialize some values to the “empName” and “emailAdrress” variables.
- Afterward, we created a method named “setEmpData()” to set/initialize some values to the “empId” and “empAge” variables.
- Next, we created a method named “showDetails()” to print the values of instance variables.
- From the main() method, we passed some values to the constructor.
- Finally, we invoked the setEmpData(), and showDetails() using the class object.
The output clarified that the instance variable is accessible within the entire class. This is how an instance variable can be created and accessed in Java.
Conclusion
In java, the instance variables are declared at the class level but outside the methods, blocks or constructors. The instance variables are accessible throughout the class i.e. within any method, block or constructor of the same class. This post considered some examples to explain what an instance variable is and how it works in Java.