-: All right, so in the last video,
we created our first project
and created our first Java class called Hello.
Let's now add a method to this new class
to print out our Hello World message.
All right, so firstly, what is a method?
Well, it's a collection of statements, one or more,
that perform an operation.
We'll be using here a special method called the main method
that Java looks for when running a programme.
It's the entry point for any Java code.
So in other words, so Java looks for that specific method
and is able to start and run the programme when we use it.
You'll see how to do that.
Now you can create your own methods,
as you'll see later, as well as this one,
but for now, let's create this special main method.
So the way to do that,
we're just gonna come up into our class on line two
and I'm gonna tab over.
I'm just gonna press Enter there.
Notice that, well, you'll see when I start typing
that IntelliJ is gonna start giving us some hints there.
So I'm gonna start by typing public static,
and notice I type S-T in the word static
and IntelliJ's suggesting the word static.
I'm just gonna press Enter there.
So anytime you see something
that you want IntelliJ to fill in for you,
just press Enter.
All right, continuing on, I'm gonna type...
Now I typed the v there because I need the word void,
and notice that IntelliJ
has automatically suggested that, as well.
I'm gonna press Enter there.
Next I'm going to type main.
Now I'm going to put a left and right parentheses.
In fact, I type the left parentheses,
and IntelliJ, being ever helpful,
added the right parentheses for us.
I'm going to type in there String with a capital S.
Case is very important when you're programming.
Left and right square braces and the word args.
Now I'm going to come over and add a left curly brace
and a right curly brace as you saw me do there.
Right, so what have we done there?
Well, firstly, looking at this statement,
public was the access modifier we discussed
when defining the class in the last video.
It's the same principle here.
Static is a Java keyword that needs an understanding
of other concepts to really be explained adequately,
so for now, just know that we need to have static
for Java to find this special method
to run the code that we're gonna be adding.
Now, void is yet another Java keyword used to indicate
that the method won't return any information,
but we're talking, again,
more about that to later.
And just a tip here that you might be perhaps thinking,
well, why can't I learn all these things now?
But there is a specific order to learning Java,
and it pays to learn things in the right order
so that things don't get too overwhelming.
Rest assured that we'll be going into all these things
in a lot of detail at the relevant point in the course.
Right, so moving on,
we've got the left and right parentheses
that I talked about,
and that is needed for a method declaration,
and that's what we're doing here.
By typing in this code, we're defining a method.
So the left and right parentheses are mandatory,
but I can optionally include one or more parameters,
and parameters are a way to pass information to a method,
and more on that, again, later.
I know I've said that a few times now, but bear with me.
All right, so here you can see
there's been one parameter that's been defined,
and we'll talk more about what that means
a little bit later.
For now, though,
to enable Java to define the main method,
it has to be typed exactly like it's typed here.
So if you typed something different,
like, for example, if you used a capital P,
now straightaway when we do that,
we suddenly get it highlighted in red,
and that's IntelliJ telling us that,
okay, I don't understand the word Public with a capital P.
But if I change that back to where it was,
which was a low one, lowercase p,
it's then quite happy and the error disappears.
So keep that in mind that often you'll find errors
can be purely relating to you typing something in uppercase
or lowercase when it should've been the opposite.
All right, so at the moment now, we've got our method,
so let's try running it.
And we can run it a few ways in IntelliJ.
At the moment we haven't got an option up here to run it.
In the top right-hand corner
to the right of where it says Add Configuration,
that run button's disabled for now,
but you'll find once we run it for the first time,
that when we run it for the first time,
that'll be enabled.
But over here,
we can click on these little buttons here to run it.
We can also right-click in there
and select Run, as well.
You can see there's even a keyboard shortcut
to do the same thing.
I'm just gonna click over here to run our programme.
Click on Run Hello.main().
You can see down the bottom, something's happening.
So what's actually happening is that IntelliJ,
via the Java Developer Kit,
is compiling the Java programme,
and you can see we've got a little bit of output
down the bottom of the screen.
Now when I say there's a bit of output,
the first line there is the line,
the command that IntelliJ sent to Java
to execute our programme,
and below that we've got this
Process finished with exit code zero,
and what that essentially means is
everything's completed successfully.
Zero is often used to indicate that there wasn't a problem.
And the reason we haven't got any other output,
our Hello world message, for argument's sake,
we haven't got that because, of course,
we haven't typed the coding yet.
But if you have a look at the main method declaration again
there on line three,
notice that the left and right curly braces,
we didn't talk about what they were.
They're actually a code block.
Now, the code block is defined,
or is used to define the start
and end of a method effectively,
and we can put our code in between those
in the form of statements.
So at the start, just to recap on that,
we got a class.
A class has got the body block of code,
which is the left curly brace
and the right curly brace of the entire class,
and our method is effectively within that class,
and it's got its own code block,
which is a group of code,
statements, if you will,
that we'll be defining
and that are unique to that specific method.
All right, so let's continue on now.
The next thing we wanna do is
we wanna print out our message,
and to do that,
we need to come back to this class,
and I'm just pressing Tab,
and I'm going to type,
so it's indented like that,
and I'm going to type System with a capital S
and a period out period println,
and noting that IntelliJ is helpfully helping us on the way.
Putting in, typing in a left parentheses,
double quote character,
and I'm going to type Hello World.
And notice that when I've done that,
we've got the right parentheses and double quotes
were added automatically by IntelliJ for us.
And I'm just gonna go to the right-hand side
and put a semicolon,
and the semicolon closes off the line to make it valid.
You'll notice that if I get rid of that,
then we immediately get an error to the right-hand side
with that little indication to say that it's an error.
And I'll put a semicolon again and the error disappears.
All right, so what we've done there
is we've added a statement.
So what is a statement?
Well, that's a complete command to be executed
and can include one or more expressions.
And we'll be talking about expressions
and a related topic, operators, later in the course.
For now, though,
what we've typed in is a command to basically call a method
that comes as part of Java called println,
and we send it the information of,
in this case, the text Hello World,
and it should print that out on the screen for us.
So let's actually try running that,
see that it works,
and notice now that up here,
the green arrow is now enabled
because we've run our code for the first time.
So I can click up there or I can come over here,
either way, to run our programme,
or I can right-click in the main method.
When I say in the main method,
making sure my cursor's within there,
and select Run.
Either of those ways'll do the same thing.
And looking at the output pane down the bottom,
you can see now that we've got
Hello World showing on the screen.
All right, so at this point,
we've actually managed to do something.
We've actually printed out the words Hello World.
Let's end the video here,
and in the next one,
I've got a challenge for you
to get you to try making a little bit of a change
to this programme to print something out.
So let's talk about that in the next video.