In this video, we're going to look at the most basic aspects of groovy programming, and that's basically
creating a class on how you get started with groovy console application, which enables you to generate
quick scripts so you can execute.
So we look at that in this video just so we can get up and running and find our way around.
So first off, as you know, in Java, every application starts with the main method.
So it's normally where you have a public static void main
argument like this.
And here we put our actual card for the program.
So you can see here, this is a public method.
It's static, which means that we don't have to create an instance of the class that exists before we
can call it.
So we can just call it on the class itself.
It's void because it doesn't return.
Anything has a well-defined name, which is men, and it takes some program arguments which are passed
in as an array of typed string.
And these usually correspond to the arguments that you pass to the program from the command line.
So usually what happens in Java programs and this will still work in Groovy as well.
So for example, in here, if I put a print statement to say running and then clear and then run, you
can see the programs running.
But we're not really concerned.
We don't have to do this because it will actually generate that method for us.
So if we get rid of that and then rerun so we can see it's all fine, it's generated it for us and we
can see this if we go into the script section, into Inspector Høst, we can see here that this is the
code that's been generated for us.
And here you can see that method we just spoke about the standard method, the financial job applications
and all groovy applications as well.
So all of the applications need to have a public static, void main method, which takes an array of
string just like any normal Java program.
And the reason this is that this is actually a job, a program behind the scenes.
It just happens to call into the groovy library code to be able to do all that groovy goodness.
So you can see here.
Org code.
How's that groovy dot runtime invoke helper run script putting in the script name, which is what is
generated, which enables the application of this short scripts which have written to actually run.
And while we're on the subject of the moment that you can actually make it shorter than usual.
So for example, if we start out with what we had before we used.
Go back to that, we can amend this slightly, so, for example, here we can take advantage of Gruber's
weak typing.
We don't have to specify what the parameter types are so we can run this.
That's fine.
Again, we don't have to specify what the return type is.
We could do this.
That's fine.
And indeed, we don't even have to market its public.
We could do that, however, we do need to say that it's static because if we do this, that won't work.
And the reason is, is that it's just things it's a normal method definition now in the script instead.
So if you go to the script, you'll see that it will have generated the public static void main method,
which it has.
But it thinks that the method that we supplied is just an average method definition here.
That's why that doesn't work.
So the very least you need to have static definition.
But what I'd suggest is always public static void main string args, because then it's crystal clear
that it's a main method.
We're sticking with convention and we're not relying on any groovy syntax tricks or anything to be quote
unquote clever to get us up and running instead of using and sticking to the de facto conventions.
Always do that.
That said.
If we take this off.
And reruns, we've got a very simple script, so the simplest possible thing you can do in a group program
is to return something.
So the simplest possible script will be, for example, just a number like this.
So when we clear and return, this is a valid, groovy script.
And this is because in Groovy, you can omit the return statement for the last statement in a method,
and that's the value that will be returned.
So this will actually be generated as return one.
And that's the simplest possible script that we can write.
Something that returns a result and doesn't actually do anything obviously wants to seem more useful,
but syntactically as the simplest possible thing that we can do.