Skip to content
Advertisement

How do you transfer a variable that is within a private method ActionPerformed to another method in Java?

I created a graphical interface in a small program in Java. When you create the fields in the graphical interface, you have automatically created the actionPerformed(java.awt.event.ActionEvent evt) methods, which are automatically private methods. I created a JTextField to receive what the user types. Then I created a string variable, such as: String inputfieldinstring = inputtext.gettext().Tostring(); Then I turned into an integer variable: int inputValueInteger = Integer.parseInt(inputfieldinstring);

How to transfer the inputValueInInteger variable, which is within the private inputTextActionPerformed (java.awt.event.aVTEvent EvT) method for another method to process data processing?

Below, I want to take the variable called inputValueInInteger that is inside the ActionPerformed(java.awt.event.ActionEvent evt), which is a private method, and transfer to other method to do the processing. How do you do this?

private void inputTextActionPerformed(java.awt.event.ActionEvent evt) {                                          
        String inputFieldInString = inputText.getText().toString();
        inputValueInInteger = Integer.parseInt(inputFieldInString);
        }``` 

Advertisement

Answer

you can declare it as a static variable:

public class MyFirstClass {

private static Integer inputValueInteger;

public Integer getInputValueInteger() {
    return inputValueInteger;
}

private void inputTextActionPerformed(java.awt.event.ActionEvent evt) {                                          
    String inputFieldInString = inputText.getText().toString();
    inputValueInInteger = Integer.parseInt(inputFieldInString);
    }
}

===========================

public class AnotherClass {

public void somethingmethod(String something) {
    Integer inputValueInInteger = MyFirstClass.getInputValueInteger();
    // Do your other thing in the method
}

Alternatively you can declare a static storage

public class Storage {
    
    private static Map<String, Integer> storageRepresentative = new ArrayList<>();
    
    public getStoreValue(String key) {
           return storageRepresentative.get(key);
    }

    public void storeValue(String key, Integer number) {
           storageRepresentative.put(key, number);
}

===============

In your MyFirstClass

public class MyFirstClass {

    private static Integer inputValueInteger;

    public Integer getInputValueInteger() {
        return inputValueInteger;
    }

    private void inputTextActionPerformed(java.awt.event.ActionEvent evt) {                                          
        String inputFieldInString = inputText.getText().toString();
        inputValueInteger = Integer.parseInt(inputFieldInString);
        //Store your values here
        Storage.storeValue("current", inputValueInteger);
        }
 }

=====================

public class AnotherClass {

    public void somethingmethod(String something) {
        Integer inputValueInInteger = Storage.getStoreValue("current");
        // Do your other thing in the method

}

It depends on your needs.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement