.... if you had to hardcode it in a Java application, what measures could you take to make it harder to extract?
To begin with, I would like to be sure that the person responsible for making this bad decision is fully aware that this is fundamentally and inevitably unsafe 1 .
Then I would probably come up with some kind of algorithm that collects the password in an obscure way; for example, by creating double-byte arrays and their XOR encoding ... and distributing entangled byte codes. The best you can hope for is making it difficult for kids with limited skills to reverse engineer the password from your code.
(Encrypting a password using a reliable algorithm will not help much, because the choice of algorithm and decryption key should be built into your code. Indeed, any scheme you can dream of can be defeated with a debugger to set a breakpoint at the point where Password must be clear.)
1 ... and that even John Skeet cannot provide security.
If some methods are better than others (for example, JPasswordField stores the password in an array of characters instead of String) ...
I just want to note that the usual reason for using a char array is to store passwords in JPasswordField, etc. It consists in protecting against bad guys reading passwords from core dumps or swap files. This will not help much in this case, because we must assume that the bad guy you should care about is someone else with access to the system administrator. He or she will have sufficient control to attach the debugger to the JVM and capture bytes from the char array.
source share