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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:54:29+00:00 2026-06-15T11:54:29+00:00

how to reference an object from string in groovy? I have many objects –

  • 0

how to reference an object from string in groovy? I have many objects – reviewSyd1, reviewSyd2..reviewSyd30 that i need to add to object – sydney. I have loop

for(i in 1..30){
  sydney.addToReviews("reviewSyd$i")
}

This is not working. how to make it work?

Thank You

Updated to clarify(sorry if i wasn’t clear)

class Review{
   String email
   String Content

}

def rev1 = new Review(email:'one@homail.com',content:'i really like it')
def rev2 = new Review(email:'two@homail.com',content:'i really like it')
def rev3 = new Review(email:'three@homail.com',content:'i really like it')
def rev4 = new Review(email:'four@homail.com',content:'i really like it')

for(i in 1..4){
   println rev$i.email
}

How to reference the object -revX where x is the iterator?

  • 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-15T11:54:30+00:00Added an answer on June 15, 2026 at 11:54 am
    println rev$i.email    
    

    Not that dynamic is groovy. This not a valid groovy syntax.
    Put those object, you want to access in later in your code,in a collection.

    def revs = []
    revs << new Review(email:'one@homail.com',content:'i really like it')
    revs << new Review(email:'two@homail.com',content:'i really like it')
    revs << new Review(email:'three@homail.com',content:'i really like it')
    revs << new Review(email:'four@homail.com',content:'i really like it')
    

    then you will have references to them:

    revs.each{
       println it.email
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I somehow got an object reference from an other class: Object myObj =
I want to return a reference of an object from a vector, and the
I need to call a Foxpro (VFP 8) COM object from C# The vfp
I have to execute a given String as JavaScript code, e.g. eval('Foo.setMessage(Hello!)') , from
WCF service on IIS. I have got a problem with reading connection string from
i'm trying to deserialize a Movie object from a German xml string: string inputString
I need to return multiple STRING values from my backgroundworker in each loop, so
I have a lot of objects of type ContainedClass stored in an object of
When a value type is boxed, it is placed inside an untyped reference object.
Exception: Object reference not set to an instance of an object. Code: For Each

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.