TupleConstructor annotation in Groovy

25 / Sep / 2012 by Mohit Garg 0 comments

@TupleConstructor annotation provides the classical constructor with default properties. It will create the constructor with first parameter as address, second parameter as name etc. Constructor implementation depends upon the order of the variable declaration. One thing you need to ensure that you need to pass the value in same order as used in variable declaration.

Here is the example:

import groovy.transform.TupleConstructor
@TupleConstructor
class Person {
    String address
    String name
    String city
    int age
}

Person person1 = new Person('ADDD','MOHIT','CITY',1)

println("name:::"+person1.name)
println("adreess:::"+person1.address)
println("city:::"+person1.city)
println("age:::"+person1.age)

Hope this code will help you :)

Tag -

Groovy
FOUND THIS USEFUL? SHARE IT

Leave a comment -