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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:15:23+00:00 2026-06-17T10:15:23+00:00

I would like help understanding how Groovy manages scope and variables when passed between

  • 0

I would like help understanding how Groovy manages scope and variables when passed between functions. Say I declare def foo in my main method and pass it as an argument to a private void method, changeStuff. Then I can make changes like follows:

public static void main(args) {
  def foo = [:];
  changeStuff(foo);
  println(foo);
}
private static void changeStuff(foo) {
  foo.bar = "new stuff";
}

The result printed is [bar:new stuff] But I have a hard time manipulating foo in other ways. See these next two examples:

public static void main(args) {
  def foo = [:];
  changeStuff(foo);
  println(foo);
}
private static void changeStuff(foo) {
  def newStuff = [:]
  newStuff.extra = "extra stuff";
  foo = newStuff;
}

prints: [:]

public static void main(args) {
  def foo = "before";
  changeStuff(foo);
  println(foo);
}
private static void changeStuff(foo) {
  foo = "after";
}

prints before

I know there is some concept here that I am not fully understanding, maybe related to def? Any summary or direction on where I can learn more about this is appreciated.

  • 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-17T10:15:24+00:00Added an answer on June 17, 2026 at 10:15 am

    My experience in groovy is very limited, so I might be off a bit.

    In the first case you mention, you are passing foo by reference to changeStuff, and inside the method, you directly modify the map, so changes are visible from your main method.

    In the second case, the parameter foo inside your changeStuff method is being assigned to another map. However, the variable foo inside your main method still points to the first map you have created, thus the empty map when you print it.

    The third case is the same as the second case. Plus, you have to be aware that String objects in Java (and probably in Groovy too) are immutable. So when “modifying” a String, what you are really doing is creating a new instance each time.

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

Sidebar

Related Questions

I would like some help in understanding a particular behaviour of java Filters: I
I would like some help understanding how to deal with isstringstream objects. I am
I would like to say that Im struggling with understanding the portal demo in
I would like some help about an Android Application. I used the new Google
I would like some help parsing a dynamic (folder.subfolders) xml with xslt, I tried
I would like to help out my girlfriend - she needs the specific count
I am trying to write a query and would like some help if possible.
I'm new to the site. would like some help if at all possible. I
i would like a simple help... i have a url like this: example.com/profile.php?id= &
i would like you to help me. I want to create dynamicly table (screenshot).

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.