How Do You Invoke a class in a Groovy Program?

Problem scenario
You have a Groovy with these lines of code:

class Contint {
   static void main(String[] args) {
      def apple = {println "This is a basic test."};;
println "This is the second print statement in the code."

The "Contint" class is never invoked.  What should you do?


You need to use the "new" reserved word.  This will instantiate the class.  The object that is created can invoke this "main" method with the .main() syntax.  

1.  Add these two lines under the last line of the program:

test = new Contint()

2.  After the program has been saved, run the program again.

3.  You are done.  How Do You Create a Class for Objects with Many Data Members and Access One Specific Data Member in Groovy?
shows two ways of instantiating a class.  It uses the above method with the "new" keyword as well as a way that does not use the "new" keyword.

Leave a comment

Your email address will not be published. Required fields are marked *