Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

Instance variable In Java: All you need to know

Last updated on Nov 29,2022 133K Views

26 / 72 Blog from Java Core Concepts

All of you are well acquainted with the concept of variables in Java which is integral to Java career or an eventual certification. Java provides us with the liberty of accessing three variables, i.e., local variables, class variables, and instance variables. In this article, I would be discussing the implementation of instance variable in Java. Below are the points that will be discussed:

Let’s begin!

What is instance variable in Java?

Instance variables in Java are non-static variables which are defined in a class outside any method, constructor or a block. Each instantiated object of the class has a separate copy or instance of that variable. An instance variable belongs to a class. 

You must be wondering about what exactly is an Instance? Let me help you by simplifying it.

When you create a new object of the class you create an instance. Consider, if you have a STUDENT class, then

class Student
{
String studentName;
int studentScore;
}

And if you create two STUDENT objects like,

Student student1 = new Student();
Student student2 = new Student();

Then two instances of the class Student will be created.

Now each student would have his own name and score right? So the value that is stored inside ‘studentName’ and ‘studentScore’ would vary for different students, they are called ‘variables’. And like you saw that these variables hold their own value for each instance, they are called Instance Variables in Java.

Now that you have understood the meaning of Instance variables, let’s move a step forward.

I will enlist the features of instance variables, which would help you in using them in a java code with ease.

Features of an instance variable?

The life of an instance variable depends on the life of an Object, i.e., when the object is created, an instance variable also gets created and the same happens when an object is destroyed.

  • Instance Variable can be used only by creating objects
  • Every object will have its own copy of Instance variables
  • Initialization of instance variable is not compulsory. The default value is zero
  • The declaration is done in a class outside any method, constructor or block 
  • Instance variables are used when the variable has to be known to different methods in a class
  • Access modifiers can be assigned to instance variables

After attaining theoretical knowledge, you might be pondering on how to implement Instance variables in Java! Let’s understand that in our next topic.

How do you implement an instance variable in Java?

Implementation of Instance variables in Java is quite easy. I have written a simple code that will help you to grasp the technical usage.

Here is a detailed code :


package Edureka;

import java.util.Scanner;

public class Student
{

public String name;

private int marks;

public Student (String stuName) {
name = stuName;
}
public void setMarks(int stuMar) {
marks = stuMar;
}

// This method prints the student details.
public void printStu() {
System.out.println("Name: " + name );
System.out.println("Marks:" + marks);
}

public static void main(String args[]) {
Student StuOne = new Student("Ross");
Student StuTwo = new Student("Rachel");
Student StuThree = new Student("Phoebe");

StuOne.setMarks(98);
StuTwo.setMarks(89);
StuThree.setMarks(90);

StuOne.printStu();
StuTwo.printStu();
StuThree.printStu();

}
}

OUTPUT:

Name: Ross
Marks:98
Name: Rachel
Marks:89
Name: Phoebe
Marks:90

Explanation:

In the above code, as you can see I have created three instance variables, namely, ‘StuOne’, ’StuTwo’, ’StuThree’. Likewise, you can create as many variables as you need depending upon your requirement. Now as we move further accumulating facts about instance variable, let me also elaborate to you the differences between an instance variable and a class variable!

Difference between an instance variable and a class variable

To clarify the differences, I have jotted down a few points that will help you to discard any ambiguity between the two.

Instance VariableClass Variable

Every object will have its own copy of instance variables, hence changes made to these variables through one object will not reflect in another object.

Class variables are common to all objects of a class, if any changes are made to these variables through object, it will reflect in other objects as well.

Instance variables are declared without static keyword.

Class variables are declared with keyword static

Instance variables can be used only via object reference.

Class variables can be used through either class name or object reference.

With this, we have reached towards the end of the blog. I hope that the contents of this article proved to be beneficial to you. We’ll keep exploring the Java world in the upcoming blogs. Stay tuned!

Now that you have understood What is Instance variable in Java”, check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring. If you’re just beginning, then watch at this Java Tutorial to Understand the Fundamental Java Concepts.

If you wish to learn more about Java, you can refer to the Java Tutorial.

Got a question for us? Please mention it in the comments section of this Instance variable in Java” blog and we will get back to you as soon as possible or you can also join our Java Training in Makassar..

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Instance variable In Java: All you need to know

edureka.co