0

How to use arraylist if I have 2 java class. Login and profile. If 2 user logs in, after submit button it will go to another java class and then will display their name, age, address. Thank you so much

3
  • Then use ArrayList<Object> or use a common interface Commented Jul 14, 2020 at 19:29
  • Does this answer your question? Create an ArrayList with multiple object types? Commented Jul 14, 2020 at 19:30
  • You need to explain more clearly and in more detail what you are trying to achieve. Your Question is unclear. Commented Jul 14, 2020 at 19:58

2 Answers 2

2

What you might want to consider is the service locator pattern. You could provide a simple service that is composed of the List and then given as a dependency to other classes.

class ArrayListService {

    private final List<MyObject> list;

    public ArrayListService(ArrayList<MyObject> list) {
        this.list = list;
    }

    public List<MyObject> getList() {
        return list;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

-2

One thing that might work is making the ArrayList public and static. This way you will be able to access it from anywhere in the application (i.e. any class).

More on static.

1 Comment

Viable solution but definitely not conventional or best practice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.