Hello team!
In this section we will learn with you more about methods in Java.
We are going to have multiple lessons in this section from which you will learn what is method and what
is method signature, how to declare methods and how to use them.
You will understand how methods can return values.
I will explain you how data is passed in Java by value and not by reference.
How to write recursive methods and what are variable length arguments?
Let's start.
What is method?
A method is a block of code which is referred to by name, and can be called at any point in your program
simply with the methods name.
Think of a method as a chunk of code, which can be performed on the request.
When during the program execution method call is performed, the program proceed execution from that
method and when method is finished, execution returns to the line of code in your program from where
method was called.
Let's write method together.
Each method has access modifier.
We will learn them during the object oriented programming topic.
Let's write private.
As of now, this means that we can access this method only in scope of this class.
After that, let's write static.
Static keyword means that this method is connected not with instances of this class, but with class
itself.
We'll have separate lessons where we will take a look at what a static and non-static methods.
While we don't know OOP.
Let's create static methods.
Here we specify method return type.
Each method can return some value after it finished.
The result of the method execution can be assigned to variable of the same type as return type.
And here is method name.
Method names are usually verbs started with lowercase letter and written in camel case like this one,
for example.
It is very important how you name your methods.
For example, guess what does this method do?
It is straight and clear what this method does.
You can save a lot of time for your colleagues and a lot of money for your company.
Who pays for time of your colleagues if you will write clean code?
Understanding of code which was written not by you can take up to 50% of time dedicated for development.
That's why in this and in future lessons, I will put stress on clean code design.
Because clean code is something that you can't learn in one day after you learned programming.
It is something that you should learn constantly.
After we wrote Methodname, we need to specify method parameters.
Here we declare that this method can work with two integers, and int values can be passed as arguments
to this method.
Method name and method parameters together is called method signature.
Why return type or access modifier is not part of method signature?
The reason for this emphasis on just the method name and parameter list is because of overloading.
What is overloading?
Overloading it is the ability to write methods that have the same name but accept different arguments.
The Java compiler is able to discern the difference between the methods through their method signatures.
For example, we have method sum which can sum two arguments.
This method can sum two integers and this method can sum to doubles.
We can say that method sum is overloaded because we have two methods with the same name, but with different
signatures.
After we declared parameters, we should put braces.
This will be our method body which will be executed each time we will call method.
If method has return type, we should return some value from the method.
To return value from the method, we need to write return keyword and specify either variable name or
expression which has compatible type with the one specified in method declaration.
It can be that method returns nothing.
For example, here are methods which just prints values to console.
They should return nothing because they just print values to console.
In this case you should specify void as return type like this.
And here you can find examples of the method invocations I call method some and it returns value.
So I can initialize this int variable with the value which was returned from this method.
Is it clear?
I can call method and can pass to it the result of execution of another method like this.
That means that this method will be executed first, and after that this method will be executed with
value, which was returned from some method.
You can find that printer console is also overloaded method which works with different types string,
double and int.
That's all.
Thanks a lot for your attention.
See you in the next lesson.
Us.