Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>static</strong> means it belongs to the class not an instance, this means that there is only one copy of that variable/method shared between all instances of a particular <strong>Class</strong>.</p> <pre><code>public class MyClass { public static int myVariable = 0; } //Now in some other code creating two instances of MyClass //and altering the variable will affect all instances MyClass instance1 = new MyClass(); MyClass instance2 = new MyClass(); MyClass.myVariable = 5; //This change is reflected in both instances </code></pre> <p><strong>final</strong> is entirely unrelated, it is a way of defining a once only initialization. You can either initialize when defining the variable or within the constructor, nowhere else.</p> <p><strong>note</strong> A note on final methods and final classes, this is a way of explicitly stating that the method or class can not be overridden / extended respectively.</p> <p><strong>Extra Reading</strong> So on the topic of static, we were talking about the other uses it may have, it is sometimes used in static blocks. When using static variables it is sometimes necessary to set these variables up before using the class, but unfortunately you do not get a constructor. This is where the static keyword comes in.</p> <pre><code>public class MyClass { public static List&lt;String&gt; cars = new ArrayList&lt;String&gt;(); static { cars.add("Ferrari"); cars.add("Scoda"); } } public class TestClass { public static void main(String args[]) { System.out.println(MyClass.cars.get(0)); //This will print Ferrari } } </code></pre> <p>You must not get this confused with instance initializer blocks which are called before the constructor <em>per</em> instance.</p>
    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