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?

JavaScript

Advertisement

Answer

you can declare it as a static variable:

JavaScript

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

JavaScript

Alternatively you can declare a static storage

JavaScript

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

In your MyFirstClass

JavaScript

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

JavaScript

}

It depends on your needs.

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