Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In general, <code>static</code> means "associated with the <em>type</em> itself, rather than an <em>instance</em> of the type."</p> <p>That means you can reference a static variable without having ever created an instances of the type, and any code referring to the variable is referring to the exact same data. Compare this with an instance variable: in that case, there's one independent version of the variable per instance of the class. So for example:</p> <pre><code>Test x = new Test(); Test y = new Test(); x.instanceVariable = 10; y.instanceVariable = 20; System.out.println(x.instanceVariable); </code></pre> <p>prints out 10: <code>y.instanceVariable</code> and <code>x.instanceVariable</code> are separate, because <code>x</code> and <code>y</code> refer to different objects.</p> <p>You <em>can</em> refer to static members via references, although it's a bad idea to do so. If we did:</p> <pre><code>Test x = new Test(); Test y = new Test(); x.staticVariable = 10; y.staticVariable = 20; System.out.println(x.staticVariable); </code></pre> <p>then that would print out 20 - there's only one variable, not one per instance. It would have been clearer to write this as:</p> <pre><code>Test x = new Test(); Test y = new Test(); Test.staticVariable = 10; Test.staticVariable = 20; System.out.println(Test.staticVariable); </code></pre> <p>That makes the behaviour much more obvious. Modern IDEs will usually suggest changing the second listing into the third.</p> <p>There is no reason to have a declaration such as</p> <pre><code>private final int NUMBER = 10; </code></pre> <p>If it cannot change, there is no point having one copy per instance.</p>
    singulars
    1. This table or related slice is empty.
    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