Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9164891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:47:24+00:00 2026-06-17T14:47:24+00:00

I have a class A which contains: List aList = new ArrayList(); aList .add(1);

  • 0

I have a class A which contains:

List aList = new ArrayList();

aList .add("1");
aList .add("2");
aList .add("3");

Now class A.java contain a reference of another class B.java and class B.java contain another list with get() and set() method.

List bList;

In class A.java i am creating object of class B.java and calling setmethod of bList and giving A.java list to this method.

B b = new B();
b.setBList(aList);

But if aList is changed the value of bList also changed. How can i create a list even changing main*(aList)* list reference list will not change?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:47:26+00:00Added an answer on June 17, 2026 at 2:47 pm

    You need to make what is called a defensive copy:

    b.setBList(new ArrayList(aList));
    

    Note that this only copies the list itself, not the objects inside. So if someone calls a mutating method on the first object of the list, these changes will still be visible to B (obviously not a problem in your example, because Strings are immutable).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object which contains a list of another object as follow: class
I have a Node class that has an 'element' slot which contains a list
I have a list which contains a simple object of a class, say Person
I have a List which contains some values. Example: List<object> testData = new List
I have a nestedGridTemplate which contains one object of a list. this.Gridparent.MasterTableView.NestedViewTemplate = new
I have a class which contains a static collection that can be used across
I have a class which contains 5 properties. If any value is assingned to
Hi i have a class which contains different methods for checking database values. Each
I have a parent class which contains a child object. I am using set
I have an abstract class which contains method for setting header text. It looks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.