Global variables in Java

To define Global Variable you can make use of static Keyword

public class Example {
    public static int a;
    public static int b;
}

now you can access a and b from anywhere by calling

Example.a;

Example.b;

You don't. That's by design. You shouldn't do it even if you could.

That being said you could create a set of public static members in a class named Globals.

public class Globals {
   public static int globalInt = 0;
   ///
}

but you really shouldn't :). Seriously .. don't do it.

Tags:

Java