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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:58:17+00:00 2026-05-17T02:58:17+00:00

I thought I understood variable scope until I came across this bit of code:

  • 0

I thought I understood variable scope until I came across this bit of code:

private static void someMethod(int i, Account a) {
  i++;
  a.deposit(5);
  a = new Account(80);
}

int score = 10;
Account account = new Account(100);
someMethod(score, account);
System.out.println(score); // prints 10
System.out.println(account.balance); // prints 105!!!

EDIT: I understand why a=new Account(80) would not do anything but I’m confused about a.deposit(5) actually working since a is just a copy of the original Account being passed in…

  • 1 1 Answer
  • 1 View
  • 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-05-17T02:58:18+00:00Added an answer on May 17, 2026 at 2:58 am

    The variable a is a copy of the reference being passed in, so it still has the same value and refers to the same Account object as the account variable (that is, until you reassign a). When you make the deposit, you’re still working with a reference to the original object that is still referred to in the outer scope.

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

Sidebar

Related Questions

I really thought I understood Python variable referencing, so I'm confused why this code
After I thought that I've understood how they work, I tried this: NSString *str1
This is something I never truly understood in Java. Lets say I have: int
I've a variable scope problem and I don't understand why this occurs and how
I thought I understood sed but I guess not. I have the following two
Being a newbie I thought I understood what to do from a security standpoint
I don't claim to know anything about svn, but I thought I understood how
Initially I thought this was going to work, but now I understand it won't
Thought my range of search options would easily find this. I wish to combine
Possible Duplicate: Child Scope & CS0136 C# Variable Scoping Though I have been using

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.