Over a million developers have joined DZone.

Groovy Goodness: Direct Field Access in (Super) Classes

DZone's Guide to

Groovy Goodness: Direct Field Access in (Super) Classes

Curious as to how to gain direct access to fields in a class's parent in Groovy? Read on to find out how!

· Java Zone
Free Resource

Managing a MongoDB deployment? Take a load off and live migrate to MongoDB Atlas, the official automated service, with little to no downtime.

When we use the property syntax of Groovy to get the value for a property, Groovy will actually try to invoke a get method for that property if it is available. So, for example, if we have the statement user.name, actually, user.getName() is invoked.

If we want to reference a property field directly, so bypassing the get method, we must place an @ in front of the property field name. In the previous example, we would write user.@name to get the field value directly. The same rules apply for setting a value for a property with the Groovy syntax. If we write user.name = 'mrhaki', then actually user.setName('mrhaki') is invoked. We can use the @ prefix as well to set a value without invoking the set method for that property. So, in our example, it would be user.@name = 'mrhaki', and the setName method is not used.

In the following example, we have a class Person with a name property. We add a getName method, which formats the name field and returns the value. In a subclass, User, we access the name property from the super class using the Groovy property syntax and with the @ prefix:

class Person {
    // We must not leave out a modifier,
    // because without a modifier, 
    // Groovy would add
    // a getName and setName method if
    // they are not already available.
    // But Groovy also adds a private modifier
    // for this property and being
    // private it is not accessible from
    // subclasses.
    protected String name

    String getName() {

    void setName(String name) {
        this.name = "*${name}*"

class User extends Person {
    String getUsername() {
        // .name will invoke getName().

    String getUsernameField() {
        // .@name will access name field.

def u = new User(name: 'mrhaki')

assert u.username == 'User(_*mrhaki*_)'
assert u.usernameField == 'User(*mrhaki*)'
assert u.name == '_*mrhaki*_'
assert u.@name == '*mrhaki*'

// Set field value directly
u.@name = 'mrhaki'

assert u.username == 'User(_mrhaki_)'
assert u.usernameField == 'User(mrhaki)'
assert u.name == '_mrhaki_'
assert u.@name == 'mrhaki'

Written with Groovy 2.4.7.

MongoDB Atlas is the easiest way to run the fastest-growing database for modern applications — no installation, setup, or configuration required. Easily live migrate an existing workload or start with 512MB of storage for free.

groovy ,access ,java

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}