‫Instructor: Hi.
‫Within this lecture we are going to focus on methods.
‫So far we have been working with methods already
‫like this onCreate onResume, onStop, onPause.
‫So these are all methods.
‫They are all related to activity life cycle
‫but of course we can create our own methods
‫or we can use other methods as well.
‫So what are methods?
‫Methods are piece of codes.
‫We use them in order to be more structural
‫like more tidy up in our code.
‫Like we can create a method in here
‫and do something inside of that method,
‫let me show you how.
‫We're going to start with public keyword.
‫So as you can see there are other keywords
‫like protected and stuff.
‫We are going to look into that.
‫And then we are going to add void.
‫So void means nothing.
‫Okay? So what we are doing here is to define what kind
‫of thing this method will return to us.
‫So if there is an int over here for example
‫this method will return an integer as a result.
‫But if you see void over here, it means that this method
‫will do something but we will not get anything in return.
‫So you will understand what this exactly means in a minute.
‫Don't worry because we are going to do a lot
‫of examples regarding methods.
‫Right now we want to start with a simple method.
‫So I'm going to say public void testMethod.
‫And open and close parenthesis and open and close curly
‫braces as well.
‫So as I said before, it creates a coding block.
‫So this is our block, this is our coding section
‫for this testMethod.
‫Whatever we write over here under this testMethod
‫will get executed once this method is called.
‫So for an example, let me create an integer in here
‫and say x is 4 + 4 and print out this integer.
‫Okay, I'm gonna say value of x and I'm going to
‫do a plus sign over here and say x.
‫So this will print out the value of x
‫once this method is executed.
‫For example, if I run this right now, we won't see any value
‫in the logcat.
‫Why? Let's see.
‫As you can see, we don't see the value of x even though
‫our app is running right now.
‫Why we don't see it?
‫Because this test method isn't even executed.
‫As you can see, if I hover
‫over here it says that it's never used
‫because we have never used it in order to use it.
‫We can assign it to some view like we did
‫in the image changing app in the beginning of this section.
‫Or we can call it anytime anywhere we want.
‫For example, we can call this under onCreate
‫or we can call this under onResume
‫or onPause or onStop or wherever we want inside
‫of another method, another class maybe.
‫So in order to do that, we just have to write its own name.
‫For example, if we write, View view like we did before
‫we can assign it to some kind of a button or an image view
‫and over here we can just call it from the onCreate.
‫And in order to do that
‫just write it and it will appear over here.
‫And if you double click on that, it will be just like this.
‫And this means executing test method under onCreate.
‫So whenever I run this application from scratch
‫onCreate will be called and we will see the value of x.
‫As you can see, it's even printed
‫out before the onResume is called
‫because that's when onCreate is called.
‫Remember the first method that we see
‫in the activity life cycle?
‫Of course we are just printing out some values as a test
‫but we could have changed the text.
‫We over here or we can do whatever we want with the user
‫interface in the XML as well.
‫And it would happen before even user sees it.
‫Okay? So that's the beauty of working with methods.
‫You write whatever you want to do inside
‫of a method and you can call it anywhere you want.
‫Now I can call this test method in another place as well
‫if I want, right?
‫Or now that I have the content of the test method
‫I can call it and just execute it in a way that I
‫want anywhere.
‫For example, we can just call them from onResume onPause
‫onStop.
‫I'm not gonna do that.
‫You can just try it on your own.
‫But I'm going to show you another features
‫another advantages of writing your own methods.
‫For example, you can take in parameters or you
‫can return something as a result as I mentioned
‫at the beginning of this lecture.
‫So let me create one
‫of that example as well, like public int.
‫I'm gonna call this math, okay?
‫So this is a math method
‫and it will return some number to me.
‫So since it's expecting an integer, it gives me an error
‫because I haven't used return keyword yet.
‫So in order to make this appropriate
‫I'm going to say return something
‫and that something should be an integer.
‫So you can just come
‫over here and say return 8, return 10, return 1 million,
‫okay, and it will return it.
‫Of course, it doesn't make sense to return eight
‫in a method like this.
‫Maybe you can do some kind
‫of a mathematical operation and return to result of it.
‫And we're gonna see what that is.
‫Don't worry.
‫For example, you can do something like this
‫four plus four and it'll give you eight as a return.
‫But what can you do more is to take some parameters
‫and return the result of those parameters as well.
‫So over here, as you can see under onCreate
‫we have some inputs and we can create those inputs
‫in here in our own method as well.
‫For example, if I say: "int a, int b",
‫then whenever or wherever I use this math
‫it will ask me for two parameters
‫and then I can return the result of those two parameters.
‫So now when I use math, it will ask me two numbers
‫and when I give two numbers to math
‫it will sum them up and show me the result.
‫For example, come over here under
‫onCreate and print out the result of math.
‫And as you can see, math is now recognized
‫as a method and it asks for an integer a and integer b.
‫Now you can double click on that and give two numbers.
‫All you have to do is just choose your numbers like three
‫and five.
‫And when you run this, you will see eight as a result
‫of this math method.
‫So let's see if we can see eight.
‫Here you go.
‫Now before even onResume code, we managed to
‫do a calculation and see the result as well.
‫So let me create another one that returns string
‫for example, rather than int.
‫I'm gonna call this newMethod to be fairly simple.
‫And as you can see, it expects
‫a string from us.
‫Otherwise it'll give me an error, it won't even compile it.
‫So for simplistic sake
‫it takes an input named "string" and it returns this string.
‫Maybe it combines it with something
‫like "new Method" or "test Method",
‫okay, like this.
‫So we can use this newMethod to add something
‫to a string.
‫This is clearly a real life example where
‫you would actually want to add something
‫to an input from user, for example.
‫So let me try to
‫print this out and say new method.
‫As you can see, it expects a string.
‫I'm gonna give a string.
‫So double quotation marks as usual.
‫Okay? And between the double quotation marks
‫I'm gonna write my name, Atil,
‫you can try it with your own name as well.
‫Just run it.
‫And in the logcat you will see, "Atil new Method".
‫So it works as you can see with help
‫with the help of method, with the features of methods.
‫You can take in parameters, you can return values
‫if you want, or you may not take them at all
‫or you may not return anything if you don't want.
‫So they're really powerful.
‫So let's stop here and continue with the next one.