Hello Groovy

I just received a complimentary e-book Groovy In Action from Manning Publications and decided to start working my way through the book.

I will do a full book review once I have finished it.

I skipped the typical install described in the book and went directly for the Eclipse Plugin, this way I killed two birds with one stone; got an IDE with syntax highlighting AND groovy installed.

I have to make a confession before going any further.

I am a notorious skimmer

After installing the Groovy plugin I promptly found myself on page 36 showing how one builds a class in Groovy. I didn't just fastforward to page 36 I skimmed ahead (big difference!), which meant I already knew that you can compile a plain old script without needing static-void-main() or any such nonsense.

Groovy says: Hello World

It's sad but it's neccessary; you aren't allowed to start programming a new language without this one simple ritual. The hello world "application". Which is one thing that is missing in the Groovy in Action book, but I wont' hold it against the author. After 50 or so pages I'm pleased with how the book is progressing.

I created my hello.groovy file and added the following extremely complex code to create my hello world application.


hw = 'Groovy says: Hello World!'
println hw
Groovy says: Hello World!

Sweet! I am a GOD! Ok well perhaps that's a little overstatement but yeah that's it no include, no static-main-void simple and to the freaking point! YEAH BABY! (I may have had one quadruple ristretto too many today)

A Groovy Class

So now I'm a groovy expert after doing hello world I figured lets make a class! Well since on the hello world trip I thought what better than to create a HelloWorld groovy class.


public class HelloWorld{
    
    private String greeting='Hello all you Groovy People!'
    
    String getGreeting(){
        return greeting
    }
}

Now I have my very unexciting class I can use it, so back to my hello.groovy file and a little change in the code.


HelloWorld hw = new HelloWorld()
println hw.getGreeting()

Hello all you Groovy People!

And there you have it a very simple Groovy class but hey it's a start. Next on the agenda is closures...

Happy Coding...

Related Blog Entries

5 Comments to "Hello Groovy"- Add Yours
Dan Vega's Gravatar Ha (I am a God), nice one. Can't wait to see more on this!
# Posted By Dan Vega | 8/22/08 9:14 PM
Dan Vega's Gravatar Did you have any issues installing the plugin? I am running Eclipse 3.4 and it did not like the grials / TestNG plugins so I ended up just installing the groovy plugin.
# Posted By Dan Vega | 8/22/08 9:41 PM
Gary's Gravatar The plugin installed without issue, though I have only played with the groovy party so far, havent touched grails yet.
# Posted By Gary | 8/22/08 12:47 AM
Sean Corfield's Gravatar You can simplify your HelloWorld class to just:

class HelloWorld { // public is the default for classes

String greeting = "Hello all you Groovy People!"
// private is the default for attributes

// public getters are automatically generated
}

Then your hello.groovy file can either continue to do hw.getGreeting() or simply hw.greeting since that automatically invokes the (automatically generated) getter as well.

If you want greeting to be readonly, provide a private setter that does nothing (yes, nothing! Groovy essentially ignores private so you can still call private methods - they just don't appear in the public class API).
# Posted By Sean Corfield | 8/22/08 3:55 AM
Gary's Gravatar Baby steps Sean, baby steps :)

Let me learn how to run before I learn how to fly with the big boys.
# Posted By Gary | 8/23/08 3:19 PM

Powered By Railo

Subscribe

Subscribe via RSS
Follow garyrgilbert on Twitter Follow me on Twitter
Or, Receive daily updates via email.

Tags

adobe air ajax apple cf community cfml coldfusion examples ext flash flex google javascript max2007 max2008 misc open source programming railo software technology ui

Recent Entries

No recent entries.

Blogroll

An Architect's View
CFSilence
Rey Bango
TalkingTree

Wish List

My Amazon.com Wish List