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

  • Home
  • SEARCH
  • 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 6561521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:35:52+00:00 2026-05-25T13:35:52+00:00

I need to perform some initialization when new instances of my domain class are

  • 0

I need to perform some initialization when new instances of my domain class are created.

class ActivationToken {
    String foo
    String bar
}

When I do this I want bar to be initialized by code inside ActivationToken:

def tok = new ActivationToken(foo:'a')

I cannot see how to ‘override’ the ‘constructor’ to make this happen. I know in this case I could just add a normal constructor but this is just a simple example.

  • 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-05-25T13:35:53+00:00Added an answer on May 25, 2026 at 1:35 pm

    The map constructor is coming from Groovy – not Grails in this case. I did some experimentation, and this is what I came up with:

    class Foo {
        String name = "bob"
        int num = 0
    
        public Foo() {
            this([:])
        }
    
        public Foo(Map map) {
            map?.each { k, v -> this[k] = v }
            name = name.toUpperCase()
        }
    
        public String toString() {
            "$name=$num"
        }
    }
    
    assert 'BOB=0' == new Foo().toString()
    assert 'JOE=32' == new Foo(name:"joe", num: 32).toString()
    

    Basically, it appears that you’ll have to manually override the constructors if you need to process the property after construction.

    Alternately, you can override individual setters, which is cleaner and safer in general:

    class Foo {
        String name = "bob"
        int num = 0
    
        public void setName(n) {   
            name = n.toUpperCase()
        }
    
        public String toString() {
            "$name=$num"
        }
    }
    
    assert 'bob=0' == new Foo().toString()
    assert 'JOE=32' == new Foo(name:"joe", num: 32).toString()
    

    Note that the default value isn’t processed, but that should be OK in most instances.

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

Sidebar

Related Questions

I have a class that I need to perform some actions on but I
I need to perform some basic OLS regression using F#. To do this I
I need to perform some string manipulation to a columns contents in a GridView,
I need to perform some action when my application receives focus. I've tried hooking
I need to perform some operations on all folders within a file share which
I'm developing an application through Core Data and I need to perform some calculation
I have a lotus view that stores a number. I need to perform some
I'm currently maintaining some web server software and I need to perform a lot
I need some advice on techniques to perform page redirect in asp.net. Which one
In a function, I need to perform some logic that requires me to call

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.