Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Variable in Java?
3.
What is Instance Variable in Java?
3.1.
Syntax
4.
How to Access Instance Variables?
5.
Features of Instance Variable in Java 
6.
Advantages of Instance Variable in Java
7.
Disadvantages of Instance Variable in Java 
8.
Default Values of Instance Variables in Java
9.
Example of Instance Variable
9.1.
Java
10.
When to use Instance Variables in Java?
11.
Difference between Class and Instance Variables
12.
Frequently Asked Questions
12.1.
What are instance variables?
12.2.
What are the examples for instance variables in Java?
12.3.
What are the 3 types of variables in Java?
12.4.
Where are instance variables stored in Java?
13.
Conclusion
Last Updated: Mar 27, 2024
Easy

Instance Variable in Java

Author Rishabh
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

In Java, variables can be declared in different sections of the code, and based on this, their memory location can change. For example, instance variables are stored in the heap memory segment and are unique to each class object. On the other hand, class variables are shared by all class objects.

instance variable in java

So, let’s dive deep into the instance variable in Java.

Read More, addition of two numbers in java

What is a Variable in Java?

A variable is a named storage location used to store data that can change during the execution of a Java program. Variables provide you a way to work with and manipulate data in a program. In Java, there are three types of variables:-

  • Instance Variables
     
  • Class Variables
     
  • Local Variables

 

In the next section, we will discuss instance variables in Java.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is Instance Variable in Java?

An instance variable is defined inside a class but outside of any constructor, method, or block. They are stored in the heap section of the memory. It is used to represent that each object of the class should have its own copy of the instance variable. For example, each time you create a new class object, it will have its copy of the instance variables.

Syntax

Instance Variables are defined using the syntax. 

class ClassName {
   datatype variableName;
}


where,

  1. Class is the access modifier. 
     
  2. The datatype is the data type of the instance variable, such as int, double, or float.
     
  3. variableName is the instance variable specified.
     

We will look into more examples as we go further.

How to Access Instance Variables?

We usually access instance variables in 2 ways

  1. Using “this” keyword: This keyword in Java can be used to reference the current object's instance variable in a constructor or call another constructor with arguments. 
     
  2. Using object references: Objects can be stored and manipulated within a class using object references that can be assigned to instance variables in Java.

Features of Instance Variable in Java 

  • An instance variable is specified inside a class but outside of any constructor, method, or block.
     
  • They are unique to each instance of the class.
     
  • They can be accessed and modified by any method or constructor in the class.
     
  • They can be accessed and modified using the dot notation, such as objectName.instanceVariableName. 
     
  • They can have different access modifiers, such as private, public, or protected.
     
  • They are destroyed when the object is destroyed.

Advantages of Instance Variable in Java

Let's look at some of the advantages of Instance Variable in Java:

  • They can be accessed by any method of the class, which can be very useful for accessing data between methods.
     
  • They are used to create relationships between objects.
     
  • Management of data structures can be done using instance variables in Java.

Disadvantages of Instance Variable in Java 

Let's look at some of the disadvantages of Instance Variable in Java:

  • They take up a lot of memory.
     
  • It cannot be declared static, abstract, synchronised, or native.
     
  • They can be prone to errors if they are not properly mentioned or can also be difficult to understand if not written in a  proper way. 

Default Values of Instance Variables in Java

Let’s look at some of the default values of instance variables in Java:

Data Types  Default Value 
int 0
boolean false
double 0.0d
object null
char \u0000
long 0L
float 0.0
byte 0
short 0

Now let’s look at a code to understand the instance variable in Java.

Example of Instance Variable

The code written below defines a class called “occupation”. The class has two instance variables, “name” and “job”. The class has a method called “printInfo” which prints the name and occupation of a person. 

Finally, there is a main method that creates an occupation object called job1 and the printInfo method is then called, which prints the name and occupation of the person.

  • Java

Java

public class Occupation {
   // Instance variables
   String name;
   String job;

   // Constructor
   public Occupation(String name, String job) {
       this.name = name;
       this.job = job;
   }

   // Method to print the occupation of person
   public void printInfo() {
       System.out.println("Name: " + name);
       System.out.println("Occupation: " + job);
   }

   public static void main(String[] args) {
       // Create a Person object
       Occupation job1 = new Occupation("John", "business");
     
       // Print the function
       job1.printInfo();
   }
}

Output 

Name: John
Occupation: business


Also read Java Ioexception

When to use Instance Variables in Java?

Instance variables in Java are used whenever we need to represent the state of an object and its properties. They are declared inside a class but outside of any method. The value of an instance variable can vary from object to object. It is also used whenever we need to share data between methods in a class.

Instance variables allow you to create more flexible and reusable classes in Java. They are also suitable for scenarios where different objects of the same class must store and manage individual data independently. 

Difference between Class and Instance Variables

Now, let’s look at some of the differences between class variables and instance variables.

Class Variables Instance Variables
It is declared with a ‘static’ keyword outside any method. It is declared without a ‘static’ keyword outside any method and declared within a class.
Memory is allocated for the variable once per class. Memory is allocated for the variable once per object.
It is accessible to all the objects of the class. It is accessible to the object to which it belongs.
It is limited to the class in which it is defined. It is limited to the object to which it belongs.
It is generally used with a class name prefix to differentiate it from instance variables. It is used with descriptive names that reflect the purpose of the variable.

Frequently Asked Questions

What are instance variables?

Instance variables in Java are data members of a class that store unique data for each object created from that class. They define the object's state and are stored in the heap segment of memory.

What are the examples for instance variables in Java?

An example of an instance variable in Java includes attributes specific to objects, such as "name" and "age" in a "Person" class, or "balance" in a "BankAccount" class. These variables hold object-specific data.

What are the 3 types of variables in Java?

In Java, there are three types of variables -- instance variables, which belong to object instances; class variables, which are shared among all instances of a class; and local variables, which are declared within methods and have limited scope.

Where are instance variables stored in Java?

Instance variables in Java are stored in the heap memory, which is allocated to each individual object created from a class. Each object has its own copy of instance variables.

Conclusion

This article discussed the basic syntax of instance variable in Java and the difference between class and instance variables in Java.

In conclusion, instance variables in Java are important for representing the state and attributes of objects within a class. They are defined within the class but outside methods or constructors and stored on the heap segment of memory. Each object created from the class has its own copy of instance variables.

Previous article
Introduction to Variables
Next article
Introduction to Data Type
Live masterclass