DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Groovy Goodness: Direct Field Access in (Super) Classes

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!

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Oct. 18, 16 · Code Snippet
Like (3)
Save
Tweet
Share
4.81K Views

Join the DZone community and get the full member experience.

Join For Free

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() {
        "_${name}_"
    }

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

class User extends Person {
    String getUsername() {
        // .name will invoke getName().
        "User(${this.name})"
    }

    String getUsernameField() {
        // .@name will access name field.
        "User(${this.@name})"
    }
}

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.

Groovy (programming language)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • Why Does DevOps Recommend Shift-Left Testing Principles?
  • A Guide To Successful DevOps in Web3
  • The Future of Cloud Engineering Evolves

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: