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
🌐
Java Concept Of The Day
javaconceptoftheday.com › home › class variables and instance variables in java
Class Variables And Instance Variables In Java
April 30, 2023 - While methods represent the behavior of an object, variables represent the state of an object. Variables in Java are of two types – Class Variables and Instance Variables. Class variables (or static variables) are common to all instances of a class where as instance variables (or non-static variables) are specific to an object.
🌐
TutorialsPoint
tutorialspoint.com › instance-variables-in-java
Instance variables in Java
February 24, 2020 - 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 is created.
Discussions

What is an instance variable in Java? - Stack Overflow
My assignment is to make a program with an instance variable, a string, that should be input by the user. But I don't even know what an instance variable is. What is an instance variable? How do I ... More on stackoverflow.com
🌐 stackoverflow.com
Why declare an instance variable private when your going to use a public getter method?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
8
2
May 23, 2021
How to set up instance variables of the object in Java? - Stack Overflow
This question is not connected to a concrete problem but is rather a general one. In Java, when creating a class, there are actually two options how to enable access to the instance variables of that More on stackoverflow.com
🌐 stackoverflow.com
Instance variables in Kotlin and Java
val foo: String = "foo" in Kotlin is the same as private final String foo_field = "foo"; public String getFoo() { return foo_field; } Likewise, var foo: String = "foo" is the same as: private String foo_field = "foo"; public String getFoo() { return foo_field; } public void setFoo(value: String) { foo_field = value; } So you don't need to make your own backing fields and setters/getters, val and var include them for you. Check here for more info: https://kotlinlang.org/docs/properties.html The other thing is Kotlin does not have implicit defaults for types like Java does (like String's default value is null), so you must always initialize properties to a valid value. You usually initialize it when you declare the property but you can also do it in an init block. More on reddit.com
🌐 r/Kotlin
9
8
September 18, 2022
🌐
Medium
medium.com › @barbieri.santiago › understanding-static-and-instance-variables-in-java-f53173cabe8c
Understanding Static and Instance Variables in Java | by Santiago | Medium
January 26, 2025 - A static method cannot directly access an instance variable. This is because static methods belong to the class, whereas instance variables belong to specific objects. Without an object reference, the instance variable does not exist in the static context.
🌐
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.
🌐
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.
🌐
Scaler
scaler.com › home › topics › instance variable in java
Instance Variable in Java - Scaler Topics
June 2, 2023 - Instance variables are declared in the class, but outside of the constructors, methods, or blocks of the particular class. Learn more on Scaler Topics.
🌐
Wikipedia
en.wikipedia.org › wiki › Instance_variable
Instance variable - Wikipedia
January 19, 2026 - In class-based, object-oriented ... but is non-static. In C++ or Java language, an instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks....
Find elsewhere
🌐
Great Learning
mygreatlearning.com › blog › it/software development › java instance: what is an instance variable in java?
Java Instance: What is an Instance Variable in Java?
January 6, 2025 - An instance variable in Java is a variable that is declared in a class but outside any method, constructor, or block. Read more.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › classvars.html
Understanding Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory.
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
Data Type: Defines the kind of data stored (e.g., int, String, float). Variable Name: A unique identifier following Java naming rules.
Published   November 11, 2025
🌐
CodingNomads
codingnomads.com › what-is-an-instance-variable-java
What is an instance variable in Java?
In Java, an instance variable is a variable that is bound to the instance of a class rather than being associated with a particular method or block of code. Each instance of the class has its own copy of the instance variables.
🌐
Rootstack
rootstack.com › en › blog › java-variables
Types of variables in Java | Rootstack
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....
🌐
Carmatec
carmatec.com › home › understanding java instance variables: a comprehensive guide with examples
Understanding Java Instance Variables: 2025 Guide with Examples
September 10, 2025 - In Java, an instance variable is a variable defined within a class but outside any method, constructor, or block. These variables are associated with instances (objects) of the class, meaning each object of the class has its own copy of the ...
🌐
Carnegie Mellon University
cs.cmu.edu › afs › cs › academic › class › 15212-s98 › www › java › tutorial › java › javaOO › classvars.html
Understanding Instance and Class Members
When you declare a member variable such as aFloat in MyClass: ... you declare an instance variable. Every time you create an instance of a class, the runtime system creates one copy of each the class's instance variables for the instance.
🌐
TutorialsPoint
tutorialspoint.com › what-are-class-variables-instance-variables-and-local-variables-in-java
What are class variables, instance variables and local variables in Java?
September 13, 2023 - Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class. Local variables ? Local variables are declared in methods, constructors, or blocks.
Top answer
1 of 6
7
This is the best analogy I have for this: say you make a Person class. One of the properties you put on this class is "Age". Rather than making getters setters, you just make public fields. Other programmers on your project begin to use your Person class, referring to your Person.age field in countless places. It gets pointed out to you that hard coding the age isn't as versatile as storing the birthdate of the Person allowing that age to be dynamically calculated. You quickly make this change, adding Dob to your Person class, and adding an "age" method to return their age based on the dob. But here's the problem. Lots of programmers already used your age field. Now all usages of your class need code updates in the code base. If you had used a getter method from the start, that wouldn't be the case. You would simply change your age method implementation and none of the usages in the code base would require changing. It boils down to the fact that methods provide a lot more abstraction than fields. So why not start with them just in case you need to change your implementation in the future? It costs you nothing other than some more typing (or if using a proper IDE, just a quick click to "generate getter/setters") and the runtime cost is minimal to non-existant as the Just-in-time compiler will likely inline the getter setter calls. Using methods keeps the door open to implementation change, while using fields shuts that door. Practically speaking it may seem like you don't need to bother in 90% of cases and you may be right, but why shut the door to flexibility especially when it costs you nothing?
2 of 6
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
W3Schools
w3schools.com › java › java_variables.asp
Java Variables
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... Variables are containers for storing data values.
🌐
GeeksforGeeks
geeksforgeeks.org › java › types-of-java-variables
Types of Java Variables - GeeksforGeeks
October 3, 2025 - Example: This example demonstrates the use of instance variables, which are declared within a class and initialized via a constructor, with default values for uninitialized primitive types. ... import java.io.*; class Geeks { // Declared Instance Variable public String geek; public int i; public Integer I; public Geeks() { // Default Constructor // initializing Instance Variable this.geek = "Sweta Dash"; } // Main Method public static void main(String[] args) { // Object Creation Geeks name = new Geeks(); // Displaying O/P System.out.println("Geek name is: " + name.geek); System.out.println("Default value for int is "+ name.i); // toString() called internally System.out.println("Default value for Integer is: "+ name.I); } }