Groovy dump() method

Often you would like to know the values of attributes of an object. Groovy provides @ToString AST transformation to generate customized toString() implementations. But dump() method can be handy here. Here is how you use it.

<br />class Person{

    String firstName

    String lastName

}

def p = new Person(firstName: 'Naresha', lastName: 'Bhat')

println p.dump()

Output:

<Person@189c30e6 firstName=Naresha lastName=Bhat>

 

Generating toString() method in groovy using AST

toString() is one of the very useful methods in Java which can be used to know the current state of an object.

class Person{
String firstName
String lastName
}

def p = new Person(firstName: 'Naresha', lastName: 'Bhat')
println p

The default behavior of toString is to print the class name followed by the object’s hash code.

 Person@7e3d3158

By applying the @ToString annotation, we can customize the toString behavior.

import groovy.transform.ToString

@ToString(includeNames=true)
class Person{
String firstName
String lastName

}

def p = new Person(firstName: 'Naresha', lastName: 'Bhat')
println p

This would output

Person(firstName:Naresha, lastName:Bhat)

For more details check http://groovy.codehaus.org/gapi/groovy/transform/ToString.html

Marching ahead with Java 7

A beginners guide to Test Driven Development

First attempt to include a code in wordpress

Testing code snippet post

public class TestClass{
  public static void main(String[] args){
    System.out.println("Hello World..!");
  }
}

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Follow

Get every new post delivered to your Inbox.