Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda