Android Studio中Java字符串值跨类传递的实现方法问询
Hey there! Since you're new to Java and working on testing vulnerable mobile apps with your own tools, I get that you want a simple way to pass the decrypted password from your Decrypt class to Decrypt2 without overhauling your existing code. Here are 3 straightforward approaches you can implement quickly:
1. Static Variable (Quickest for Testing Scenarios)
If you're just doing testing and don't need to worry about thread safety or strict OOP practices, a static variable is the easiest fix. It lets you share the password globally between the two classes with minimal code changes:
// In your Decrypt class: public class Decrypt { // Add a public static variable to store the decrypted password public static String decryptedPassword; public void runDecryption() { // Your existing decryption logic here String password = /* result of your decryption */; // Assign the decrypted password to the static variable decryptedPassword = password; } } // In your Decrypt2 class: public class Decrypt2 { public void decryptSecretMessage() { // Access the password directly from Decrypt's static variable String password = Decrypt.decryptedPassword; // Use this password to decrypt your secret message } }
Note: Static variables are shared across all instances of a class, so this works best for simple, single-threaded test code.
2. Constructor Parameter (More OOP-Friendly)
If you want a cleaner approach without global variables, pass the password to Decrypt2 when you create an instance of it. This only requires adding a constructor to Decrypt2 and modifying how you initialize it in Decrypt:
// In your Decrypt class: public class Decrypt { public void runDecryption() { String password = /* result of your decryption */; // Pass the password directly to Decrypt2's constructor Decrypt2 decryptTool = new Decrypt2(password); decryptTool.decryptSecretMessage(); } } // In your Decrypt2 class: public class Decrypt2 { private String password; // Add a constructor that accepts the password public Decrypt2(String password) { this.password = password; } public void decryptSecretMessage() { // Use the stored password for decryption usePassword(this.password); } private void usePassword(String password) { // Your secret message decryption logic here } }
This keeps the password encapsulated within Decrypt2 and avoids global state, which is better practice for even small projects.
3. Method Parameter (Most Flexible)
If you already have an instance of Decrypt2 created elsewhere, just modify its decryption method to accept the password as a parameter. This is the smallest change if you don't want to touch constructors or add static variables:
// In your Decrypt class: public class Decrypt { public void runDecryption() { String password = /* result of your decryption */; // Assume you already have a Decrypt2 instance Decrypt2 decryptTool = new Decrypt2(); // Pass the password directly to the decryption method decryptTool.decryptSecretMessage(password); } } // In your Decrypt2 class: public class Decrypt2 { // Modify the method to accept the password as a parameter public void decryptSecretMessage(String password) { // Use the passed password to decrypt your secret message } }
This approach keeps things flexible—you can call the decryption method with different passwords whenever needed, without tying Decrypt2 to a single password instance.
If your existing code has a specific structure (like the classes running independently, or using a framework), feel free to share snippets of your Decrypt and Decrypt2 code, and I can tweak these suggestions to fit exactly what you're working with!
内容的提问来源于stack exchange,提问作者cbates79

