im just confusing how to access my array list from another frame to another frame for example:
Class Customer
{
String name ="";
int age = 0;
}
Class Customerlist
{
ArrayList<Customer> customerlist;
}
and i have 3 Frame let say (main frame ,frame 1 and frame 2
in frame 1 i create object from class Customer and Customerlist
Customer myCustomerObj = new Customer();
Customerlist myCustomerlistObj = new CustomerList;
myCustomerlistObj.customerlist.add(myCustomerObj);
in frame 2 i create again object from class Customer and Customerlist
Customer myCustomerObj = new Customer();
Customerlist myCustomerlistObj = new CustomerList;
myCustomerlistObj.customerlist.add(myCustomerObj);
now i want to check the size of my arraylist in main Frame
Customerlist myCustomerlistObj = new CustomerList;
with -> myCustomerlistObj.customerlist.size();
as result the size is 0, but when i check the size in frame 1 and frame 2 i get size 1
this frame 1 and frame 2 are called with button. im sorry for my bad english
and what is the purpose make a static attribute like
static private Custumer cs;
Every time when you creating new instances from CustomerList class, a new ArrayList object is create on memory.
Customerlist myCustomerlistObj = new CustomerList;
Every time this code line executes, it creates a new instance of ArrayList. So if you want to access single ArrayList from all frames you should re create your CustomerList class like this.
public class CustomerList{
public static ArrayList<Customer> customers = new ArrayList<>();
}
After that you can access the list like this.
CustomerList.customers.add(new Customer());
And also you asked what static keyword is. static attributes are bind to the class. Not its instance. How many instances you create from that class, static attribute will create a single copy in the memory.
Your ArrayList should be public static. If you do that you should be able to access your ArrayList from another class like this:
Customerlist.customerlist.add(new Customer());
int size = Customerlist.customerlist.size();