How can I ensure the destruction of a String object in Java?

前端 未结 9 974
感情败类
感情败类 2020-11-30 04:36

An empoyee at my company needs to modify data from a SQL Server database through a program I made. The program used Windows authentication at first, and I asked the DBAs to

9条回答
  •  [愿得一人]
    2020-11-30 05:11

    I can only think of a solution using reflection. You can use reflection to invoke the private constructor that uses a shared character array:

      char[] chars = {'a', 'b', 'c'};
      Constructor con = String.class.getDeclaredConstructor(int.class, int.class, char[].class);
      con.setAccessible(true);
      String password = con.newInstance(0, chars.length, chars);
      System.out.println(password);
    
      //erase it
      Arrays.fill(chars, '\0');
      System.out.println(password);
    

    Edit

    For anyone thinking this is a failproof or even useful precaution, I encourage you to read jtahlborn's answer for at least one caveat.

提交回复
热议问题