Class Variables

Class Variables

Class variables (or static variables) are declared with the static keyword within in a class, but outside a method.

There can only one version of a class variable within a class and it will be shared with all instances of that class. If changes are made to a class variable, all other instances will also change.

public class Name{
//The following class variable will be seen by every method within this class
    public static int firstName;

    public static void main(String[] args) {
    System.out.println(firstName);
}
}

Leave a Reply