javaglobal-variables

How to have global values inside a class?


I want less methods. I want a common global TestClass from which I could use any of its value inside the class.

import java.util.*;
import java.io.*;

public class TestClass {
    TestClass(String hello){
        String hallo = hello;
        String halloSecond = "Saluto!";
    }

    public static void main(String[] args) {
        TestClass test = new TestClass("Tjena!");
        System.out.println("I want "Tjena!": " + test.hallo);
        TestClass testSecond = new TestClass("1");
        System.out.println("I want Saluto!:" + test.halloSecond);
        System.out.println("I want Saluto!:" + testSecond.halloSecond);

        // How can I get glob.vars like the "Saluto!"?
    }
}

[Clarification Needed] I cannot understand the no-use of GLOB.VARS. Please, see the code below where you cannot access the GLOB.VARS without an instance, hence the error. If I guarantee no malicious code can make an instance, is there any problem in using GLOB.vars?

$ javac TestClass.java 
TestClass.java:19: non-static variable hallo cannot be referenced from a static context
  System.out.println("It did not get to the GLOB.VAR: " + hallo);
                                             ^
1 error

With this class:

import java.util.*;
import java.io.*;

public class TestClass {

    public String hallo;
    public String halloSecond;

    TestClass(String hello){
        hallo = hello;
        halloSecond = "Saluto!";
    }

    public static void main(String[] args) {
        TestClass test = new TestClass("Tjena!");
        System.out.println("It did not get to the GLOB.VAR" + hallo);
    }

}

Solution

  • I guess you are looking for something like this:

    public class TestClass {
        public final String hallo;
        public static final String halloSecond = "Saluto!";
    
        TestClass(String hello){
            String hallo = hello;
        }
    
        public static void main(String[] args) {
            TestClass test = new TestClass("Tjena!");
            System.out.println("I want "Tjena!": " + test.hallo);
            TestClass testSecond = new TestClass("1");
            System.out.println("I want Saluto!:" + test.halloSecond);
            System.out.println("I want Saluto!:" + testSecond.halloSecond);
        }
    }
    

    The value of hallo is set in each instance of TestClass. The value of halloSecond is a constant, shared by all instances of the class and visible for the whole app. Note that with this code your IDE/compiler probably gives you a warning upon test.halloSecond - it should be qualified by the class name, like TestClass.halloSecond, rather than an instance name.

    Update on global variables: the main problem with global variables is that they make the code

    In Java everything is inside a class, so you can't have "classic" global variables like in C/C++. However, a public static data member is still in fact a global variable.

    Note that the code sample above, halloSecond is a global constant, not a variable (as it is declared final and is immutable), which alleviates much of these problems (except maybe the dependency issue).