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 7712917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:44:38+00:00 2026-06-01T01:44:38+00:00

Consider this code: package Prova; import java.util.ArrayList; public class Prova { private ArrayList<String> people;

  • 0

Consider this code:

package Prova;

import java.util.ArrayList;

public class Prova
{
    private ArrayList<String> people;
    public Prova() {
        people=new ArrayList<String> ();
    }

    public ArrayList<String> getPeople (){
        return people;
    }

    public static void main(String[] args) {
        Prova p=new Prova();
        p.go();
    }

    public void go(){
        ArrayList<String> temp=getPeople();
        temp.add("jack");
        System.out.print(getPeople());
    }
}

It prints “jack”.

Why? Doesn’t this violate encapsulation? How to return it by value?

  • 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-01T01:44:39+00:00Added an answer on June 1, 2026 at 1:44 am

    You need to program defensively. There are a few alternatives to consider

    • Don’t expose the list externally, expose methods to apply to the list instead, e.g.,
    public void addPerson(String personName) {
        people.add(personName);
    }
    
    • Return immutable objects or a copy of the object. E.g.,
    public List<String> getPeople {
         return new ArrayList<String>(people);
    }
    

    As far as the why goes, it’s as already explained by other posts. The value to the reference of the ArrayList is passed (alas changing the value doesn’t change the original reference). However the list itself contains modifiable references to its objects.

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

Sidebar

Related Questions

Here is my code.... package calendar; import java.util.*; /** * This class represents a
Consider code sniper below: package sync; public class LockQuestion { private String mutable; public
Let's consider the following code in Java. package obj; final class First { public
Consider this code: public class Main { static String s = -; public static
Consider this code: template <typename T> class String { public: ... String(T* initStr) {
consider this code block public void ManageInstalledComponentsUpdate() { IUpdateView view = new UpdaterForm(); BackgroundWorker
Consider this code (Java, specifically): public int doSomething() { doA(); try { doB(); }
Consider this code: public <T> List<T> meth(List<?> type) { System.out.println(type); // 1 return new
Consider this code: import scala.xml.{Node,HasKeyValue} def domatch(x:Node): Node = { val hasBar = new
Consider this code : class App { public static function log($msg) { echo $msg;

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.