Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
accessing an ArrayList another class

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;
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!