Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An instance variable is a variable that is a member of an instance of a class (i.e associated with something created with a <code>new</code>), whereas a class variable is a member of the class itself.</p> <p>Every instance of a class will have its own copy of an instance variable, whereas there is only 1 of each static (or class) variable, associated with the class itself.</p> <p><a href="http://www.programmerinterview.com/index.php/c-cplusplus/whats-the-difference-between-a-class-variable-and-an-instance-variable/">difference-between-a-class-variable-and-an-instance-variable</a></p> <p>This test class illustrates the difference</p> <pre><code>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 1 //(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" } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload