mercredi 21 août 2019

Using if statement in static block in Java

I have a static block that I want to initialize some parameters/variables that my class will use.

I have a condition in my code to change a parameter after it loads based on whether or not a particular string is in the code.

I'm trying to use the contains String method, which seems to work outside of the if statement, but when I use it as the condition in the if statement, it does not work. I feel like there is something fundamental that I am missing here. Been banging my head against the wall trying to understand this.

class Main {
  private static String JDBC_URL;
  static {
    try {
      JDBC_URL = "jdbc:postgresql://blahstageblah.csdfhglx.ut-1.rdsdr.adc.com:9999/psfb";
      System.out.println(JDBC_URL.contains("stage"));
      if (JDBC_URL.contains("dev") || JDBC_URL.contains("staqe")) {
        System.out.println("Contains!");
      } else {
        System.out.println("Does not contain!");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public static void main(String[] args) {
  }
}

/* Prints out */
true
Does not contain!

What's going on here? I don't understand why the contains method works by itself, but won't enter into the first part of my if statement.

Aucun commentaire:

Enregistrer un commentaire