Groovy classes and scripts
Groovy has done a lot to make Java more palatable, but at the same time has introduced some confusion as to what developers can get away with at the most fundamental level.
Living on The Main Line
In Java, main methods are typically, if not always
If you have a valid main method you will see the runnable arrow
Groovy scripts
I have some sturdy bags I take with me to the grocery store. If I buy lot of food they just give me a paper bag in which to put the extra items. Every item is treated the same regardless of which bag they put it in.
A Groovy script is essentially any file with some code outside the confines of a class. Scripts can contain their own classes but all the stuff outside those is given its own class based on the filename and placed inside a working main(args) method. They just give you another bag - it's no big deal.
The following Groovy class and Groovy script are equivalent.
Groovy class with main method
Multiple main methods - you had to ask
Can classes inside a script have their own main method? Umm, yes, but that is pretty confusing (even to IntelliJ). I would have to explicitly call that main method from my class to use it - main is not a constructor.
Class with its own main method.
IntelliJ again
Both Groovy scripts and Groovy classes with main methods should be run as Groovy Script in the Run/Debug Configuration. Sometimes IntelliJ will default to attempting to run it as a Java application. If you attempt to run a Groovy script as a Java application you will get the following error:
Living on The Main Line
In Java, main methods are typically, if not always
public static void main(String[] args)In Groovy, methods are public by default and args is typed dynamically, so you can get away with
static main(args) //ok
but notstatic main() //not ok
void main() //not ok
void main(args) //not ok
attempting to run a class with these bad main method declarations, the compiler will invariably returnCaught: groovy.lang.GroovyRuntimeException: This script or class could not be run.
It should either:
- have a main method,
- be a JUnit test, TestNG test or extend GroovyTestCase,
- or implement the Runnable interface.
IntelliJ is clever in that it can recognize the proper declaration on the fly. If you missing the required main method declaration you will get the normal Groovy class file type iconIf you have a valid main method you will see the runnable arrow
Groovy scripts
I have some sturdy bags I take with me to the grocery store. If I buy lot of food they just give me a paper bag in which to put the extra items. Every item is treated the same regardless of which bag they put it in.
A Groovy script is essentially any file with some code outside the confines of a class. Scripts can contain their own classes but all the stuff outside those is given its own class based on the filename and placed inside a working main(args) method. They just give you another bag - it's no big deal.
The following Groovy class and Groovy script are equivalent.
Groovy class with main method
class aGroovyClass {Groovy script with class
def myString = "I am an instance variable";
static main(args) {
def myObject = new aGroovyClass();//def required
println myObject.myString;
}
}
class aGroovyClassWithinAScript {
def myString = "I am an instance variable";
}
myObject = new aGroovyClass(); //no def needed
println myObject.myString;
Multiple main methods - you had to ask
Can classes inside a script have their own main method? Umm, yes, but that is pretty confusing (even to IntelliJ). I would have to explicitly call that main method from my class to use it - main is not a constructor.
Class with its own main method.
IntelliJ again
Both Groovy scripts and Groovy classes with main methods should be run as Groovy Script in the Run/Debug Configuration. Sometimes IntelliJ will default to attempting to run it as a Java application. If you attempt to run a Groovy script as a Java application you will get the following error:
Exception in thread "main" java.lang.NoSuchMethodError: main
I am very much satisfied with your blog, thanks for updating.
ReplyDeleteSoftware testing training in chennai
JAVA Training in Chennai
Hadoop Training in Chennai
Selenium Training in Chennai
web designing Training in chennai
German Classes in chennai
best german classes in chennai
Hi! I was much time to spend your post, it is very impressed to me. so I am regularly following your post. I would like more ideas from your blog...
ReplyDeletePrimavera Training in Chennai
Primavera Course in Chennai
Excel Training in Chennai
Corporate Training in Chennai
Embedded System Course Chennai
Linux Training in Chennai
Tableau Training in Chennai
Spark Training in Chennai
Oracle Training in Chennai
Thank you sharing this kind of noteworthy information. Nice Post.
ReplyDeleteEducation
Technology
Good explanation with appropriate solution.
ReplyDeleteArticle submission sites
Guest posting sites
Hi! I was much time to spend your post, it is very impressed to me. so I am regularly following your post. I would like more ideas from your blog...
ReplyDeletePython Training | Java Course in Chennai
how to get rid of cat pee smell
ReplyDeleteAre you tired of using cat litter odor eliminator that doesn't work? even the best odor eliminating cat litter, the best clumping cat litter, or any cat litter. It is frustrating isn't it? KittyVent eliminates litter box smell better than the world's best cat litter. It is the ultimate cat odor ventilator!
https://kittyvent.com/
Having perused this I trusted it was incredibly useful. I appreciate you finding the time and energy to assemble this substance. I by and by wind up expressly investing a lot of energy both perusing and leaving remarks. Yet, so what, it was as yet justified, despite any trouble!
ReplyDeletelive
much thanks to you for picking out the length to discuss this, I environment all-strong about it and love breaking down more upon this subject. it is concurred gainful for me. gratitude for this kind of basic addition toward the rear of once more. Fire Alpaca Crack
ReplyDeleteVery secure message. I basically staggered concerning your blog and wanted to broadcast that i've in all actuality cherished concentrating on your blog entries. Any gaudiness i'll buy in on your feed and I slant you plug again rapidly. tremendous gratitude for the valuable information. Spyhunter 5 Crack
ReplyDeleteLove failure quotes are for the one who failed in love. Love failure makes you realize the best moments you have lived or are living with Love Failure Quotations
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks for this blog, it is for informative.
ReplyDeletePaytm Careers
Paytm Jobs
Nice Blog
ReplyDeleteHibernate Training in Chennai
This comment has been removed by the author.
ReplyDeleteWonderful Blog
ReplyDeleteAdvanced Excel Training in Chennai
Learn Advanced Excel Online
Great blog
ReplyDeleteTCS jobs for Freshers
TCS careers
Freshers Jobs
super blog thanks for sharing like this
ReplyDeleteGoogle Jobs For Freshers
Google Careers
Freshers Jobs