Field Variables


Field Variables

🙋 Need help? Ask an expert now!

Field variables are variables that are declared as a member of a class or declared outside any method or constructor within the class. Please review the example below showing two field variables.
package exlcode;

public class FieldVariableExample {

  // these are field variables
  public static int exampleVariableOne = 10;
  public static int exampleVariableTwo = 6;

  public static void main(String[] args) {
    System.out.println(add(exampleVariableOne, exampleVariableTwo));

  public static int add(int x, int y) {
    // this is a local variable
    int exampleVariableThree = x + y;
    return exampleVariableThree;

  public static void multiply() {
    System.out.println(exampleVariableOne * exampleVariableTwo);

Field variables, as opposed to local variables, can be called in any of the methods that exist in the same class. A field variable is available as long as the instance it belongs to is active. The class is active when any method in it is used. Therefore if the field variable belongs to the class, it can be used within any of the methods inside that class.

There are two categories of field variables: instance variables and class variables. The code we reviewed above shows examples of two class variables. We see this because they are declared with the static modifier.

Instance variables are non-static fields and therefore declared without the Java reserved word static. All of these values are unique to each instance of a class. This will be explained further when we study the concept of objects.

Edit Me on GitHub!

Application Question

Which variables in the following code segment are field variables?

public class TestClass()
  int varOne = 100;
  int varTwo = 25;

  public static void main (String[] args)
    int varThree = 5;
    System.out.println("Hello World!");

  public static int exampleMethod(int paramOne)
    int varFour  = 50;