How do I create static variables in Java?

Class variables or static variables are variables that are declared with static modifier. A given class will have only one copy of each of its static variables, regardless of how many times the class has been instantiated.

If the value of a static variable is changed, the new value is available equally in all instances of the class. The final keyword could be added to indicate the value of static variable will never change.

If you try to assign a new value to final variable, you will get a compile error.

package org.kodejava.basic;

public class StaticDemo {
    // static variable with final value that never change
    final static int Y = 20;
    // static variable
    static int x = 12;
    // non-static variable
    int z;

    public static void main(String[] args) {
        StaticDemo sd0 = new StaticDemo();

        System.out.println("x before update = " + StaticDemo.x);
        System.out.println("y= " + StaticDemo.Y);

        sd0.z = StaticDemo.x + StaticDemo.Y;
        System.out.println("z= " + sd0.z);

        StaticDemo.x = 15;
        System.out.println("x after update = " + StaticDemo.x);

        StaticDemo sd1 = new StaticDemo();
        StaticDemo sd2 = new StaticDemo();
        StaticDemo.x = 20;

        System.out.println("StaticDemo.x = " + StaticDemo.x);
        System.out.println("sd0 = " + sd0.getX());
        System.out.println("sd1 = " + sd1.getX());
        System.out.println("sd2 = " + sd2.getX());

        //
        // try to assign value to final variable, it will cause a
        // compile time error
        //
        // StaticDemo.Y = 30;
    }

    public int getX() {
        return StaticDemo.x;
    }
}

Here is the output printed by the program:

x before update = 12
y= 20
z= 32
x after update = 15
StaticDemo.x = 20
sd0 = 20
sd1 = 20
sd2 = 20

How do I get environment variables?

Environment variables are a set of dynamic values that can affect a running process, such as our Java program. Each process usually have their own copy of these variables.

Now we would like to obtain the available variables in our environment or operating system, how do I do this in Java? Here is a code example of it.

package org.kodejava.lang;

import java.util.Map;
import java.util.Set;

public class SystemEnv {
    public static void main(String[] args) {
        // We get the environment information from the System class. 
        // The getenv method (why shouldn't it called getEnv()?) 
        // returns a map that will never have null keys or values 
        // returned.
        Map<String, String> map = System.getenv();

        Set<String> keys = map.keySet();
        for (String key : keys) {
            // Here we iterate based on the keys inside the map, and
            // with the key in hand we can get it values.
            String value = map.get(key);
            System.out.println(key + " = " + value);
        }
    }
}

Here are some results on my machine.

...
M2 = C:\ProgramData\chocolatey\lib\maven\apache-maven-3.5.0\bin
JETTY_HOME = C:\ProgramData\chocolatey\lib\jetty\tools\jetty-distribution-9.4.31.v20200723
USERNAME = wsaryada
ProgramFiles(x86) = C:\Program Files (x86)
M2_REPO = C:\Users\wsaryada\.m2
SPRING_HOME = C:\ProgramData\chocolatey\lib\spring-boot-cli\spring-2.2.4.RELEASE
M2_HOME = C:\ProgramData\chocolatey\lib\maven\apache-maven-3.5.0
JAVA_HOME = C:\Program Files\Java\jdk1.8.0_161
OS = Windows_NT
COMPUTERNAME = KRAKATAU
CATALINA_HOME = C:\tools\apache-tomcat-10.0.11
...