Java Global Variables: Setup Guide

In Java, global variables can be set by the following method:

  1. constant
public class MyClass {
    public static int myGlobalVariable = 10;
}
  1. accessible to the general public
  2. unmoving
public class MyClass {
    // 类中的其他代码...

}

public class AnotherClass {
    public static int myGlobalVariable = 10;

    // 其他类中的代码...
}

Please note that using global variables should be done with caution, as they can be modified from anywhere. In most cases, it is better to limit variables to the smallest scope where they are needed.

bannerAds