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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:26:36+00:00 2026-06-04T01:26:36+00:00

I have this weird bug that I cannot fix. Can anyone help me? Thank

  • 0

I have this weird bug that I cannot fix. Can anyone help me? Thank you.

ArrayList<Choices> newSecurityChoicesList =
    securityChoicesController.getChoicesList();
System.out.println("first-" + newSecurityChoicesList.size());
securityQuestion.getChoices().clear();
System.out.println("second-" + newSecurityChoicesList.size());

Some explanation:

I created this newSecurityChoicesList ArrayList. It is a local variable in a method.
The first system print out gives me a result of 2.

Why does the second print out give me a result of 0?

Of course it is because of the securityQuestion.getChoices().clear(); method.
But why can a method change a local variable in a method? This method is only called once at the very end of the application.

Thanks in advance.

  • 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-04T01:26:38+00:00Added an answer on June 4, 2026 at 1:26 am

    You are dealing with a reference variable and changing the property of the object it refers to. So while the method’s local variable will be local, it will be referring to the object that was passed in to the method. Again, this is not a bug in Java, but a problem with your understanding about reference variables.

    If you don’t want to change the state of the ArrayList passed into the method, then make a deep copy of it in the method before working with it. In other words, you’ll want to create a new ArrayList and then iterate through the parameter ArrayList, making copies of each item in the original list before adding it to the new list.

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

Sidebar

Related Questions

This is a weird bug that I can't figure out because I have tested
This has been driving me nuts all day. I have a weird bug that
I have a very weird bug that I can't seem to figure out. I
I found this very weird bug that I can't understand. First of all inside
I hope someone can help me with this weird bug. I've got a popover
This is pretty weird. I have my Profiler open and it obviously shows that
This one is weird, I have a page that consists of a html table
I have this really weird bug at my page. I wrote this comment system
Hey fellas, I have this weird bug, and I have no clue how to
I have this weird bug, where C++ calls the wrong function: So this bit

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.