Instance variable is the variable declared inside a class, but outside a method: something like:

class IronMan {

    /** These are all instance variables **/
    public String realName;
    public String[] superPowers;
    public int age;

    /** Getters and setters here **/
}

Now this IronMan Class can be instantiated in another class to use these variables. Something like:

class Avengers {

    public static void main(String[] a) {
        IronMan ironman = new IronMan();
        ironman.realName = "Tony Stark";
        // or
        ironman.setAge(30);
    }

}

This is how we use the instance variables. Shameless plug: This example was pulled from this free e-book here here.

Answer from Yash Sharma on Stack Overflow
Top answer
1 of 2
84

Instance variable is the variable declared inside a class, but outside a method: something like:

class IronMan {

    /** These are all instance variables **/
    public String realName;
    public String[] superPowers;
    public int age;

    /** Getters and setters here **/
}

Now this IronMan Class can be instantiated in another class to use these variables. Something like:

class Avengers {

    public static void main(String[] a) {
        IronMan ironman = new IronMan();
        ironman.realName = "Tony Stark";
        // or
        ironman.setAge(30);
    }

}

This is how we use the instance variables. Shameless plug: This example was pulled from this free e-book here here.

2 of 2
31

An instance variable is a variable that is a member of an instance of a class (i.e., associated with something created with a new), whereas a class variable is a member of the class itself.

Every instance of a class will have its own copy of an instance variable, whereas there is only one of each static (or class) variable, associated with the class itself.

What’s the difference between a class variable and an instance variable?

This test class illustrates the difference:

public class Test {
   
    public static String classVariable = "I am associated with the class";
    public String instanceVariable = "I am associated with the instance";
    
    public void setText(String string){
        this.instanceVariable = string;
    }
    
    public static void setClassText(String string){
        classVariable = string;
    }
    
    public static void main(String[] args) {
        Test test1 = new Test();
        Test test2 = new Test();
        
        // Change test1's instance variable
        test1.setText("Changed");
        System.out.println(test1.instanceVariable); // Prints "Changed"
        // test2 is unaffected
        System.out.println(test2.instanceVariable); // Prints "I am associated with the instance"
        
        // Change class variable (associated with the class itself)
        Test.setClassText("Changed class text");
        System.out.println(Test.classVariable); // Prints "Changed class text"
        
        // Can access static fields through an instance, but there still is only one
        // (not best practice to access static variables through instance)
        System.out.println(test1.classVariable); // Prints "Changed class text"
        System.out.println(test2.classVariable); // Prints "Changed class text"
    }
}
🌐
GeeksforGeeks
geeksforgeeks.org › java › variables-in-java
Java Variables - GeeksforGeeks
Variables in Java are named memory locations used to store data that can be accessed and manipulated during program execution.
Published: 1 week ago
Discussions

How do I access a variable from another class?
Content removed in response to reddit API policies More on reddit.com
🌐 r/java
6
0
February 17, 2014
Do subclasses inherit the data fields of the superclass?
If class Y is a subclass (extends) class X, then class Y is also a class X, therefore having all the same fields except fields that are private to class X. If X has getters and setters to its private fields, Y will have access to the private fields via those. More on reddit.com
🌐 r/java
15
0
September 20, 2014
When would you use final vs. private?
Final means that the value of the variable cannot be changed after it has first been set (ie it is a constant). Private means that the variable can only be accessed in the class in which it was declared. The two are not exclusive options. You can have a private final variable, which is a constant that can only be accessed within the class. Most of the time I use private variables, because modifying variables from other classes leads to "spaghetti code" that is problematic in large projects. When I have a value that will not change, ie a constant, I usually use public static final. Final prevents the constant from being modified, so it is safe to share its value with other classes. Static is normally added because most constants apply to all instances of the class. If a constant varies for each instance of a class, but it never changes once set, all you have to do is set its initial value in the constructor and not code any changes to it after that. If you do code changes to its value, it is usually because you need to. I can't think of a single case where not including final on a non-static variable has caused a difficult to trace error. Thus final without static is not a very useful combination. More on reddit.com
🌐 r/javahelp
7
1
August 9, 2017
(Java) How to access private instance variables from an abstract class in another class? I tried using getter and setter methods but it didnt work.

You can't. That's the whole point of private variables.

If you need to access them, the typical pattern is to have a public getter/setter method that retrieves or updates the private variable from within the class, like so:

private int x;
public void setX(int X)
{
    x = X;
}
public int getX()
{
     return x;
}

Then call the corresponding method from your "other" class.

More on reddit.com
🌐 r/computerscience
2
1
January 16, 2016
🌐
Edureka
edureka.co › blog › instance-variable-in-java
Instance Variable In Java | Java Instance Variable with Examples | Edureka
July 5, 2024 - 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.
🌐
W3Schools
w3schools.com › java › java_variables.asp
Java Variables
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... Variables are containers for storing data values.

Instance variable is the variable declared inside a class, but outside a method: something like:

class IronMan {

    /** These are all instance variables **/
    public String realName;
    public String[] superPowers;
    public int age;

    /** Getters and setters here **/
}

Now this IronMan Class can be instantiated in another class to use these variables. Something like:

class Avengers {

    public static void main(String[] a) {
        IronMan ironman = new IronMan();
        ironman.realName = "Tony Stark";
        // or
        ironman.setAge(30);
    }

}

This is how we use the instance variables. Shameless plug: This example was pulled from this free e-book here here.

Answer from Yash Sharma on Stack Overflow
Find elsewhere
🌐
Wikipedia
en.wikipedia.org › wiki › Instance_variable
Instance variable - Wikipedia
January 19, 2026 - In class-based, object-oriented ... instantiated object of the class has a separate copy, or instance. An instance variable has similarities with a class variable, but is non-static. In C++ or Java language, an instance variable is a variable which is declared in a class ...
🌐
CodingNomads
codingnomads.com › what-is-an-instance-variable-java
What is an instance variable in Java?
These variables represent the state of an object and define its properties. Instance variables are declared within a class but outside of any method, constructor, or block.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › classvars.html
Understanding Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
For example, suppose you want to create a number of Bicycle objects and assign each a serial number, beginning with 1 for the first object. This ID number is unique to each object and is therefore an instance variable. At the same time, you need a field to keep track of how many Bicycle objects have been created so that you know what ID to assign to the next one.
🌐
Quora
quora.com › What-are-instance-variables-in-Java
What are instance variables in Java? - Quora
Answer (1 of 13): Simple and straight answer is there are no types of instance variable. instance variables belongs to the instance(object). Are created everytime when we create an object of the class. Every object contains a separate copy of instance variables. In java there aare 3 types of ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › instance-variable-final-java
Instance Variable as Final in Java - GeeksforGeeks
April 25, 2025 - Since instance variables belong to objects and static blocks run before object creation, it is not possible to initialize a final instance variable in a static block. ... // Java program to illustrate that we can't initialize // final instance variable in a static block class Test { final int x; public static void main(String[] args) { // Cannot assign to non-static // final field from static context x = 10; Test t = new Test(); System.out.println(t.x); } }
🌐
Rutgers CS
cs.rutgers.edu › courses › 111 › classes › fall_2011_tjang › texts › notes-java › data › variables › 45local-inst-class.html
Local/Instance/Class Variables - Java
They are also called member or field variables. When an object is allocated in the heap, there is a slot in it for each instance variable value. Therefore an instance variable is created when an object is created and destroyed when the object is destroyed.
🌐
Coderanch
coderanch.com › t › 671935 › java › job-instance-variables-java
What is the job of instance variables in java? (Beginning Java forum at Coderanch)
October 24, 2016 - A Person object holds to pieces of data: the name of the person and the age of the person. This data is stored in the member variables 'name' and 'age'. ... Have you gone through the official tutorial that explains all this? It's here: https://docs.oracle.com/javase/tutorial/java/concepts/object.html
🌐
Electronics Reference
electronicsreference.com › home › java programming › what is a java instance variable? with examples
What is a Java Instance Variable? With Examples - Electronics Reference
August 9, 2023 - Instance variables serve as containers for storing data that are unique to each object instance of a class. They are distinct from static and local variables, and they empower developers to create dynamic and interactive applications by ...
🌐
Medium
amankseth.medium.com › what-are-the-instance-variables-static-variables-class-level-variables-and-local-variables-in-199dddaac3b7
What are the instance variables, static variables, class-level variables, and local variables in JAVA? | by Aman Kumar Seth | Medium
September 28, 2024 - Definition: Instance variables are declared within a class but outside of any method, constructor, or block. Scope: They belong to instances (objects) of the class and each instance has its own copy of these variables.
🌐
Medium
medium.com › @udarachinthaka135 › class-variable-and-instance-variable-in-java-e4017e67980e
Class variable and instance variable in Java | by Udara Chinthaka | Medium
July 6, 2024 - ඊට පස්සෙත් උඩින් වගේම object දෙක use කරලා myInstanceVar කියන variable එක print කරනවා. මෙතනදී, object1 එකට අදාලව print line එකේ output එක වෙන්නේ “java programmer” කියන එක. Object2 එකට අදාල print line එකේ output එක වෙන්නේ කලින් එකමයි ඒ කියන්නෙ instance variable කියන එක.
🌐
Medium
yadavrohit-dev.medium.com › static-vs-instance-variables-in-java-explained-with-a-simple-example-46460101a9e3
static vs Instance Variables in Java — Explained with a Simple Example | by Rohit Yadav | Medium
February 6, 2026 - If a variable represents “what an object has”, make it non-static. If it represents “what the class knows”, make it static. Once you see it this way, static stops being scary and starts making sense 🙂 · Thanks for reading! If you enjoyed this post and want to explore more of my Java insights, backend development projects, or tech experiments, feel free to check out my portfolio:
🌐
TutorialsPoint
tutorialspoint.com › article › instance-variables-in-java
Instance variables in Java
Java Java Object Oriented Programming Java Technologies · Instance variables are declared in a class, but outside a method, constructor or any block. When space is allocated for an object in the heap, a slot for each instance variable value ...
🌐
Rootstack
rootstack.com › en › blog › java variables: what they are and how to use them
Java variables: What they are and how to use them | Rootstack
February 18, 2022 - Local variable: These variables ... must be initialized before they can be used. Instance variable: These variables are declared within a class but outside any method, constructor, or block of code....
🌐
BeginnersBook
beginnersbook.com › 2023 › 03 › instance-variables-in-java-definition-and-usage
Instance Variables in Java – Definition and Usage
January 1, 2024 - In Java, an instance variable is a variable that belongs to an instance of a class, rather than to the class itself. An instance variable is declared within a class, but outside of any method, and is defined for each object or instance of the class.
🌐
Javajee
javajee.com › class-variables-instance-variables-and-local-variables-in-java
Class variables, Instance Variables and Local variables in Java | JAVAJEE.COM
September 14, 2021 - Class variables (or static variables) are declared within a class and outside all methods ... Instance variables (non-static) are specific to each object and are defined in a class outside any methods.