[MUSIC]
>> Greetings, everyone, and welcome back to
Microsoft's Java for Beginners series.
My name is Brian Benz.
I'm a Cloud advocate at Microsoft.
We're going to be talking about methods.
Specifically we're going to talk about how you call a method,
where a method goes in your application and
using parameters and variables with your methods.
This is a two part topic.
In Part 1,
we're going to talk about the concepts and in Part 2,
I'm going to show you some examples.
I'm actually just going to drive right
into the first example I want to show
you and then I'll talk about it while I'm showing you around.
Every Java application, every source file has a Java extension.
In this case it's called example1.java.
Every Java file or Java application has
at least one class and at least one main method.
In this case, we have one class,
example 1, and we have a main method here.
The JVM looks for a class when you provide it at startup,
and then it looks for the main method in a class.
The JVM runs everything between
the curly braces of your main method, which is right here.
This is really where the Java code
happens in any Java application.
Everything that you've achieved up until this point is actually
just a structure that is in place to support Java methods.
But Java methods are actually where the code goes.
In Java in general,
when people talk about statements,
they're talking about Java code that's running.
Now a class actually creates an object that can run methods,
and one or more methods can be located inside of that class.
Methods return one value that's very important to remember,
or they can return no value if you declare them as
void and I'll explain what that is in a second here.
But Java, it's important to remember returns one value.
Now you can get around that by creating
an array and returning multiple values in that array,
so that's the one thing you're returning.
But if you don't want to return anything at all,
you declare the application method void.
Let's go through here, so we've got a class Example 1,
public, static, void, main.
This is what you use for every single main method.
Public means it's accessible by everything.
Static means that it's loaded at time of run or runtime.
Void means that it doesn't return
any values because it's the main method.
Main is a reserved keyword with Java,
the JVM will look for it when it actually runs.
That's important to remember as well.
Then this is a string of arguments
that you pass that are parameters inside of a Java method.
In this case, we're not passing anything.
It's just a simple one that runs and it's just a loop
that starts with a variable of x.
It counts to four and it prints
out something about variables being returned.
Let's actually just run this and you'll see what it does.
That's all it does. Example 1,
instance variables go here before the loop.
That's right up here.
Running the loop, so it runs the loop
right here while x equals 4.
Then it prints out this is where values are returned,
if any, after the loop.
That's a really simple example,
is just one class with
one method and it's a main method running and returning nothing.
Let's look at a little bit more of
a detailed example and I'll talk
a little bit more about how you work with methods as well.
Let's go back over here.
Let's get rid of this and let's talk about the methods.
I'm going to show you an Example 2, in a second here.
But before I do that,
I want to just talk a little bit about method components.
You got a method name,
it doesn't always have to be main.
You have access modifiers which would be public,
private, protected, or by default, which means nothing.
You've got non-access modifiers,
which is a way of telling the Java compiler how
you want to handle this at runtime
and then you've got different keywords.
Now everyone's seen public, static, void,
main because that's part of a main method,
but there's other keywords you can use as well.
For example, abstracts for
templates synchronized for single-thread methods.
Java is multi-threaded.
After all, this would just tell the JVM to
handle this particular class as a single-thread.
Then you've got native and you can include non-native code
in your Java application as long as it has
a keyword native and
it will return code that Java will not interpret or compile.
Then you've got strict P,
which is one of the interesting ones.
It just works with floating point,
so it only works with accepts and
returns floating point calculations.
The advantage of it is, it's incredibly fast,
so it's very specialized and very fast type of class.
You've also got a parameter list which is
comma-delimited enclosed by parentheses.
You have to include the parentheses
even if you don't have parameters,
just make it an empty open and close.
There is an optional exception list,
which I don't show you here and then
the method body actually returns things in curly braces.
After that, you put in any return values if the method is not void.
Also, parameters and variables
need to be understood as well when you're working with this.
When a value is passed to a method, it's called an argument.
When a value is received from an argument from
another class or a method is called a parameter.
More than one argument or parameters separated by commas,
and you pass the method as an argument.
You can do that with the correct parameter types.
You can actually pass a method inside of your parameter to call
another class or another object to
return a value inside of your parameter when it's called,
as long as the correct parameter types in order
are respected inside of your code.
In addition, there are instance variables and local variables.
Instance variables are declared inside a class,
but outside a method they become part of the object and are
handled differently when you're working with
the Java memory management.
I'll discuss a little bit more in Part 2 of methods.
Local variables are declared within a method
and those are only available to that method inside.
Local variables must be initialized.
Instance variables are set to default
values so they don't need to be initialized to run,
they're part of the class versus
part of the local method that you're running.
A method uses parameters to receive the past variable,
and it can then behave like a local variable as well.
Let's have a look at that example.
In this case, just like Example 1,
we've got a public class up here.
We've got our main method,
public static, void main, it returns nothing.
It has parameters which are empty.
Then we've got another line
here that we didn't have in the previous one.
What this is a constructor,
it creates a new class instance.
In this case, it creates a object it's called
e and it's based on example 2 and
it's as constructor is called to create that new example.
Then you call another method called loop.
This is a main method calling another method.
Down here is loop,
so it does the same thing as the previous example.
But instead of everything being contained inside the main method,
now we're calling this method from the main method.
If you can see here it's not public,
it's not static, it's just void,
which means it's a default access modifier
and it's just going in it's
void so it's not going to return anything and it's just going to
run what we had in the previous example as well.
Let's just run that and see what that looks like.
As you can see here, it basically does
the same thing, is the same result.
But the advantage here is that you can call
multiple classes and create
multiple objects from inside your main method.
Now, this is more how a Java application works because
classes and objects are very important when you're working with
Java to be able to call them and instantiate them.
I could actually create
several example 2 objects
and name them different things and then run them
multiple times or I can call 20 different other classes to
create objects and return them with
a unique name so I can use them in my application.
That's the whole idea behind classes and methods.
In Part 2,
I'm going to show you a little bit more about how
we actually work with those methods in
a way more detailed and lifelike examples.
That was the concepts behind methods.
That's part 1.
In Part 2, of this topic,
I'm going to show you some examples in
action of the concepts I described here.
Please check that out and also please check out the rest of
the topics for the Microsoft's Java for Beginners series.
[MUSIC]