What is an instance variable in Java? For starters, they are more valuable than they may initially sound. They are invariably vital for your Java software. But what are they really, and how do they work?
This post will explain the concepts behind an instance variable, how they work and how you can use them. You will see some code examples of syntax and learn of some ways you can use them in your development to build better, more efficient software.
Without further delay, let's jump right in.
What is an Instance in Java?
When you create a class in Java, you will often need to create new variables to hold specific data. The class is a blueprint of an object you will later use, but that class – or blueprint – has its own data for that object. The variables inside a class are called class variables, and when you use the blueprint to create an object, it gets stored in an instance variable.
Difference Between Instance and Class Variables
Java class variables and instance variables are very different; in fact, the only thing they share is that they store data. When you create a class in Java, you are creating a blueprint for objects you will need in your software, such as tracking users. That blueprint will inevitably need to store various pieces of data as well; in the case of users, you might want to store a username, age, password, etc. This data is stored in a class variable, and these variables are often accessed in the say way that methods are accessed, using the dot notation.
The instance variable is quite different as it has access to all the public methods and variables that the class object has. Check out this next image which helps illuminate the differences between the two types of variables.
Instance Variable in Java
The instance variable is a temporary copy of the class it references and any public method, getter, setter, or variable it owns. This is an important distinction because the instance variable will be a bit more robust than a typical variable.
With instance variables, you can create as many uniquely named instances of that class as you want or need. Take the example of a “users” class that tracks the above information, username, age, and password. This data is held in class variables, but the class variable that contains the data belongs to the instance variable.
This may seem a little confusing initially but fret not because we will cover them in a little more detail with an excellent code example in just a minute. For now, check out the video below for more information on instance variables.
Instance Variable Benefits
The instance variable's benefits are unbound and only limited by your imagination. The more powerful your classes are, the more powerful the instances of that class can be. A robust user class can hold all the data you need to track a user of your software.
That doesn't mean you should cram everything related to a user into one class. But it does simplify the data structure of your software considerably.
Example of Instance Variable in Java
There is no better way to learn a programming concept than to see it in action, and to that end, below are a couple of examples of instance variables in use. The example below follows the idea above of a user class for tracking users.
The above is a class used to define the information that is related to the user's profile information. The User class consists of two class variables called userName and memberDays. The class also has two setters and a method, the setters will be used to set user information, and the method will allow us to see the data related to a user. This is just the start, a User instance still needs to be created in the main class.
Creating a user instance is simple, and you will see it probably looks familiar. In fact, you will see a striking similarity to declaring new strings, ints, and more.
And that is it. With that code in place, you would have created a User class and a single user instance stored in an instance variable named userInst – this name is arbitrary; you can call it whatever you like.
How do you call an instance method in Java?
Creating an instance variable is one thing, but there is a lot of power behind them since they have all the characteristics of the class that defines them. A method defined in the class is available to the instance variable, just like the class variables.
So, like with other classes, calling a method available to an instance variable is done using dot notation. In the code above, the class defines a method called printUser. Let’s take a closer look at what that would look like using the above example.
To call the method, you would use the following line of code:
That line of code would print out the user information added to the instance variable. This instance variable consists of the name and the number of days they have been a member.
Where are instance variables stored in Java?
In Java, instance variables get stored in the heap collection where JVM applications instantiate new objects. Heap variables and objects can be shared across threads as long as the application that created them is running, making them more accessible.
What’s Next for You and Instance Variables in Java
The information provided in this article is everything you need to know to get started using Java instance variables. Moving forward, you can further solidify the knowledge you gained here by putting these concepts into action. You can create simple software applications and add classes that can be used to create instances of objects.
Don’t forget to check out other blog posts on Java and put your skills together to create powerful Java applications.