Issue
i am using netbeans to create my GUI. I got the following class
- MainUI.java
- Person.java
- Gender.java (Enum)
Inside my MainUI.java are the codes auto generated by netbeans for my UI.
I want to create an ArrayList which will add a Person object into the arraylist when a JButton is clicked.
Where should i place the code ArrayList<Person> list = new ArrayList<Person>();
?
public class MainUI extends javax.swing.JFrame {
OR
public static void main(String args[]) {
.
If i put it in static void main, i could not access the arraylist from my eventhandler code in MainUI etends javax.swing.JFrame
Solution
Create a class where you store variables that should be accessed from anywhere in your program.
class Global {
public static ArrayList<Object> list = new ArrayList<>();
private Global(){}
}
You would access it like this:
GLobal.list.add(new Object());
Answered By - Shiro
Answer Checked By - Marilyn (JavaFixing Volunteer)