Skip to content

Share ArrayList Between Classes in Java with Code

Learn how an ArrayList can be shared between different classes in a Java program. In other words, how to pass Arraylist from one class to another in java

We’ll create an ArrayList in the main() method and will share it to the two different classes i.e. the Read and the Write class.

The Write class will add elements to an ArrayList and another Read class will read elements from the same ArrayList.

Logic is simple.

Create a list using an java.util.ArrayList in the main() of the Sample class. Pass the same ArrayList to constructor of both Read and Write class constructor and initialize the list fields of both class. Perform operations add or remove on that list.

NOTE: If two objects are performing read and write operation on same shared list, then we need to synchronize the list to avoid exception. So, we will be Synchronizing the ArrayList using Collection.synchronizdList.

Code Example to Share ArrayList between classes

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

 * This class will add elements to 
 * a shared list that is created with an Arraylist.

class Write {

	private List<String> list;

	// Receive a shared list in constructor
	Write(List<String> list) {
		this.list = list;

	// add items
	public void addItem(String item) {


 * Read class will read elements form a shared list
class Read {

	private List<String> list;

	// Receive shared list in constructor
	Read(List<String> list) {
		this.list = list;

	// Read the items
	public void readItem() {

		for (String item : list) {



// Test class.

public class Sample{
	public static void main(String[] args) {

		// Use synchronized list to avoid exception
		List<String> list = Collections
				.synchronizedList(new ArrayList<String>());

		Write w = new Write(list);
		Read r = new Read(list);



Sharing ArrayList of user defined objects from one class to Another in java

We’ll learn how to import ArrayList of objects from one class to another class in Java

In this example, we’ll create a Book class which objects will be stored into an ArrayList.

We’ll have two more classes: MainBookStore , and NewBookStore.

We’ll create multiple objects of the Book class and store into the ArrayList of the MainBookStore.

And then, get the array list of book objects from the MainBookStore and assign to the ArrayList of the NewBookStore.

And then, we’ll print the name of the book and id into the NewBookStore.

CODE Example:

import java.util.ArrayList;

class Book {

	String name;
	int id;

	public Book(String name, int id) { = name; = id;

	public String getName() {
		return name;

	public int getId() {
		return id;

class MainBookStore {
	private ArrayList<Book> books;

	public ArrayList<Book> getAllBooks() {
		return books;

	public MainBookStore() {
		books = new ArrayList<Book>();

	public void add(Book object) {

//Create a new book store and import all the 
//book list from main book store.
class NewBookStore {

	private ArrayList<Book> books;

	public void getAllBooksFromMainStore(ArrayList<Book> list) {
		// first syntax
		this.books = list;
		// Second syntax
		// this.books = (ArrayList<Book>) list.clone();


	public void printBooks() {
		for (Book b : this.books) {
			System.out.println(b.getName() + " " + b.getId());

public class BookStoreOwner {

	public static void main(String[] args) {
		// Add book objects to the ArrayList of the storage class
		MainBookStore s = new MainBookStore();
		s.add(new Book("OOP Concepts Booster", 1));
		s.add(new Book("IT Jobs Made Easy for Freshers", 2));
		s.add(new Book("Head First Design Patterns", 3));

		// Create object of new book store
		NewBookStore bs = new NewBookStore();
		// Get book list to the new Store from main store;



OOP Concepts Booster 1
IT Jobs Made Easy for Freshers 2
Head First Design Patterns 3

Published inJava collections Examples


  1. Hi Qasim, If I understand your question correctly, in technical terms, you can call it producer consumer relationship. Write class object will produce items and Read will consume. You can read more here about producer consumer relationship
    If it doesn’t answer your question, then please elaborate.

  2. Qasim

    I’m talking about the first code example.

  3. Qasim

    what will be the name of relation between these classes that ar sharing same Arraylist.


  4. Thanks for asking the question Meepa. I’ve updated the example in this post. Sorry for the late reply.

  5. Meepa


    can you give example of an Obj in an arraylist.
    That Obj has many variables, and so you may need one of them or all of them. How can you get it or print the variable in this object?
    Also, how would you import that Obj to another classes arrayList?
    The arrayList is private and in different class.


Comments are closed.