Search code examples
javaobjectnetbeansreference

JAVA 20: Setting static value to a static variable sometimes return the original value of that variable. It never happened before Java 20 update


I am trying to set a single variable that can be used as common application ID that will be set in the beginning of application loading. The original variable is set to some generic String "ROOT". Amongst my classes, only one class, when calling that variable, returns ROOT instead of the set value.

This is the class that is called the first: Main.java that will do two things: First load Properties:

private void loadProp()
{
  Constants.APP_ID = svr.getAppID(); //getAppID() returns simple String "EMS" and set to the APP_ID in the RootConstants. Constants extends RootConstants.
}

Root Constants APP_ID Declaration:

public static String APP_ID = "ROOT"; //This will be replaced with "EMS" when loadProp is called.

Second Main.java will call loadLog() method:

private void loadLog()
{
    //Initiating the log agent first
    la = LogAgent.getInstance(Constants.APP_ID);
    System.out.println("The app id: " + Constants.APP_ID + " has loaded log: " + la);

    //Adding the console log
    console = new LogWriter();
    la.addWriter(console);
}

Then, I have a class that will be instantiating the log object by passing the APP_ID that is already set in the first time the program is loaded, It should be calling the Constants.APP_ID --> which supposed to call RootConstants.APP_ID, with supposedly the newly set value of "EMS", but however, instead of returning EMS when calling Constants.APP_ID, it returns "ROOT" (original declaration value). This happens only when I call the LogAgent getInstance() at the top class level.

public class GuestDataProcessor extends RootProcessor
{
  /** Database helper for access processor. */
  private GuestDataDB db;
  
  /** Log agent to be used to log everything. */
  private LogAgent la = LogAgent.getInstance(Constants.APP_ID); //--> This will return original value of "ROOT" 

  public GuestDataBean getGuestData(String id)
  {
    la.system("Guest found: " + g.getGuestCode() + " | " + g.getName()); //--> This will show error as the log taken from wrong ID
  }
}

However, if I call the getInstance() at the method level. Everything works OK.

public class GuestDataProcessor extends RootProcessor
{
  /** Database helper for access processor. */
  private GuestDataDB db;
  
  /** Log agent to be used to log everything. */
  private LogAgent la;

  public GuestDataBean getGuestData(String id)
  {
    la = LogAgent.getInstance(Constants.APP_ID); //--> This will return The correct value of EMS
    la.system("Guest found: " + g.getGuestCode() + " | " + g.getName()); //--> This will work as intended
  }
}

Can someone explain if I miss something or if something is happening??? It literally works like 30 minutes before I made a little changes implementing interface to this class. Which has nothing to do with the constant class.

I am confused. I uses Netbeans IDE 18 with Java 20.0.0.1. And thank you in advance.


Solution

  • Doh! I was dumb. I found the answer. I instantiate a mid class called CheckIns in the Main.java. Checkins is the one that calls for the GuestDataProcessor, which also being instantiated at class level, and then GuestDataProcessor instantiate the log agent at the class level as well.

    Since java calls the class method initiation first, the set APP_ID did not get a chance to be set with new value before being called and thus returns the old value.

    Well, sorry for the mishaps.