‫Hello, my friends, and welcome back.
‫In this video, we'll learn about the methods in Java.
‫A method is a block of code that performs a specific task.
‫Suppose you need to create a program that creates a circle and color it.
‫You can create two methods to solve this problem.
‫A method to draw the circle and a method to color the circle.
‫Dividing a complex problem into smaller chunks makes your program makes your program easy to understand
‫and reusable.
‫In Java, there are two types of methods user defined methods.
‫You can create your own methods based on our requirements, and this is what we're going to learn.
‫The standard library methods.
‫These are built in methods in Java that are available to use, and we use a method before called length
‫in the string library.
‫Now let's move to declare and create a Java method.
‫The syntax to declare a method is return type method name.
‫And curly braces.
‫And here we put the method, but the return type, it specifies what type of value a method returns.
‫For example, if a method has an int return type, then it returns an integer value.
‫If the method doesn't return a value, its return type is void.
‫The method name.
‫It's an identifier ID that is used to refer to this particular method in the proc method body.
‫It includes the programming statements that are used to perform some tasks.
‫The method body is enclosed inside the curly braces, and you can notice that after the method name,
‫we place the parentheses and this is how we create the method.
‫Remember, this is the simple syntax of method declaration.
‫Now let's take an example for creating and declaring a Java method.
‫I'll comment this and I want from you to write all these notes, because these are very important notes
‫in order to understand the syntax and to to remember them later on when you review them.
‫I will start by creating a void, which is the return type keyword.
‫Say hello.
‫This is the method name.
‫Followed by the parentheses.
‫And I'll start with the curly braces.
‫And here I'll place the method body.
‫I'll place this method outside the main method in order to prevent any error.
‫Okay, so this is the main method.
‫The end of this main method is here.
‫I created this method.
‫Say hello outside the main method again.
‫This is the return type.
‫Void method name is Say hello.
‫And these are the parentheses and these are the curly braces.
‫Inside the curly braces, we specify the block of code that this method will perform when it's called
‫System.out.print line.
‫Hello.
‫From the method we created this method.
‫Now we need to call it.
‫In the main method.
‫So whenever you want to call the method and you can see noticed that the name of this method is in gray,
‫and if you hover this mouse cursor, Android studio will tell you.
‫Method say hello is never used.
‫So you didn't call say hello method in any of your codes in order to call.
‫To trigger the method, you need to name it.
‫And specify it again.
‫Say hello method.
‫And like this.
‫Here.
‫We need to add some identifiers and we have this error.
‫I'll make it as static.
‫Later on we'll talk about the static and non static variables and methods in Java.
‫But till now, focus with me.
‫This is the data type and the returning type.
‫The say hello, the name of the method, the body of the method.
‫And this is how we call the method.
‫We specify it, we specify its name and followed by the parentheses.
‫Let's run our program and see.
‫And here we go.
‫This is the message from the method.
‫So the programs runs the main method.
‫The main method called the say hello method.
‫Say hello will execute this line.
‫So this method has only one thing to execute is to print out a message.
‫Hello from the method in the output.