Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda