Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>These words all alter the way the variable to which they are applied can be used in code.</p> <p><b>static</b> means that the variable will only be created once for the entire class, rather than one for each different instance of that class.</p> <pre><code>public class MyClass{ public static int myNumber; } </code></pre> <p>In this case the variable is accessed as MyClass.myNumber, rather than through an instance of MyClass. Static variables are used when you want to store data about the class as a whole rather than about an individual instance.</p> <p><b>final</b> prevents the variable's value from changing after it is set the first time. It must be given an initial value either as part of its declaration:</p> <pre><code>public final int myNumber = 3; </code></pre> <p>or as part of the class's constructor:</p> <pre><code>public MyClass(int number){ this.myNumber = 3; </code></pre> <p>Once this is done, the variable's value cannot be changed. Keep in mind, though, that if the variable is storing an object this does not prevent the object's variable from being changed. This keyword is used to keep a piece of data constant, which can make writing code using that data much easier.</p> <p><b>private</b> modifies the visibility of the variable. A private variable can be accessed by the instance which contains it, but not outside that:</p> <pre><code>public class MyClass{ private int myNumber; public void changeNumber(int number){ this.myNumber = number; //this works } } MyClass myInstance = new MyClass(); myInstance.myNumber = 3; //This does not work myInstance.changeNumber(3) //This works </code></pre> <p>Visibility is used to control how a class's variables can be used by other code. This is very important when writing code which will be used by other programmers, in order to control how they can access the internal structure of your classes. Public and private are actually only two of the four possible levels of visibility in Java: the others are <b>protected</b> and "no (visibility) modifier" (a.k.a not public or private or protected). The differences between these four levels is detailed <a href="http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html" rel="nofollow noreferrer">here</a>.</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.
    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