How to make a global variable in Java?

To make a global variable in Java, define a public static field inside a class.

First define a class with a static field:

public class MyClass {

  public static int myGlobalVariable;
}

Then you can write and read to that field in your code:

MyClass.myGlobalVariable = 14;
System.out.println(MyClass.myGlobalVariable); // 14