Can Two Variables Refer to the Same ArrayList in Java? Last Updated : 05 Jan, 2023 Comments Improve Suggest changes Like Article Like Report ArrayList class in Java is basically a resizable array i.e. it can grow and shrink in size dynamically according to the values that we add to it. It is present in java.util package. Syntax: ArrayList<E> list = new ArrayList<>();An ArrayList in java can be instantiated once with the help of the 'new' keyword and can be referred to by various objects/variables. The figure above shows how the object 'list' points to the ArrayList inside the memory. Now let us see how we can make another object to point to the same ArrayList that is instantiated above. Syntax: Two variables refer to the same ArrayList: ArrayList<Integer> secondList = list; Now when assigned the 'list' to the 'secondList' then the 'secondList' also starts pointing to the same ArrayList inside the memory. Notice here simply create a new instance. Now, for a better understanding let's dive into code and see a few examples. Below is the implementation of the problem statement: Java // Java program to show how two objects can // refer to same ArrayList in Java import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Creating an ArrayList of Integer type in Java ArrayList<Integer> list = new ArrayList<>(); // Creating a new ArrayList object and making it // refer to the first ArrayList(list) ArrayList<Integer> secondList = list; // Inserting some Integer values to the // arrayList using list variable/object list.add(17); list.add(10); list.add(1); list.add(33); list.add(2); // ArrayList after insertions : [17, 10, 1, 33, 2] // Displaying the ArrayList System.out.print("ArrayList after insertions: "); display(list); // Modifying the ArrayList using secondList // Removing element from index 0 secondList.remove(0); // Inserting Integer variables secondList.add(51); secondList.add(99); // ArrayList after modifications using // secondList: [10, 1, 33, 2, 51, 99] // Displaying the ArrayList System.out.print( "ArrayList after modifications using secondList: "); display(list); } // Function to display an ArrayList public static void display(ArrayList<Integer> list) { for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } System.out.println(); } } OutputArrayList after insertions: 17 10 1 33 2 ArrayList after modifications using secondList: 10 1 33 2 51 99 Comment More infoAdvertise with us Next Article Can Two Variables Refer to the Same ArrayList in Java? shivamsingh00141 Follow Improve Article Tags : Java Java Programs Java-ArrayList Practice Tags : Java Similar Reads How to make ArrayList Thread-Safe in Java? In Java, Thread is the smallest unit of execution within the program. It represents an independent path of execution that can run concurrently with other threads. When dealing with multi-threaded applications, where multiple threads are accessing and modifying data concurrently, it's crucial to ensu 3 min read Reference Variable in Java Before We Started with the Reference variable we should know about the following facts. 1. When we create an object (instance) of class then space is reserved in heap memory. Let's understand with the help of an example. Demo D1 = new Demo(); Now, The space in the heap Memory is created but the ques 10 min read How to Serialize ArrayList in Java? ArrayList is a class under the collections framework of java. It is present in java.util package. An ArrayList is a re-sizable array in java i.e., unlike an array, the size of an ArrayList can be modified dynamically according to our requirement. Also, the ArrayList class provides many useful method 2 min read Comparing two ArrayList In Java Java provides a method for comparing two Array List. The ArrayList.equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal. Example: Input : ArrayLis 2 min read How to Declare an ArrayList with Values in Java? ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co 2 min read Which Data Type Cannot be Stored in Java ArrayList? The ArrayList class implements a growable array of objects. ArrayList cannot hold primitive data types such as int, double, char, and long. With the introduction to wrapped class in java that was created to hold primitive data values. Objects of these types hold one value of their corresponding prim 4 min read Traverse Through ArrayList in Forward Direction in Java ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java. The listIterator() method of java.util.ArrayList class is used to return a list iterator over the elements in this list (in a proper organized sequence). ArrayList can be 3 min read Java Program to Change a Collection to an Array An array is a data structure that can hold a fixed-size, homogeneous collection of elements of the same data type, which can be either primitive data types (e.g., int, float) or object references. However, the size of the array cannot be changed once it is created. On the other hand, a collection is 3 min read How Objects Can an ArrayList Hold in Java? ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java just as Vector in C++. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. In order to understan 3 min read Java Program to Create ArrayList From Enumeration Enumerations serve the purpose of representing a group of named constants in a programming language. Enums are used when we know all possible values at compile-time, such as choices on a menu, rounding modes, command-line flags, etc. It is not necessary that the set of constants in an enum type stay 2 min read Like