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...

Subscribe
Subscribe via RSS
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

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).
Let me learn how to run before I learn how to fly with the big boys.