43

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 create one? What does it do?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aqua
  • 719
  • 4
  • 11
  • 17

2 Answers2

79

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yash Sharma
  • 1,674
  • 2
  • 16
  • 23
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"
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Richard Tingle
  • 16,906
  • 5
  • 52
  • 77
  • correct. You can also think of an instance variable as a `field` in an object. A relevant concept is encapsulation (see: `private` access modifier, getters and setters...) – vikingsteve May 22 '13 at 08:11
  • Indeed, i have declared most things public for easy access, this is usually a bad idea – Richard Tingle May 22 '13 at 08:17