hi in this video we're going to talk more about methods in Java so we'll start by introducing methods and remember methods allow us to break down our code into reusable parts so we can make our code easier to understand by breaking it into methods we can avoid writing repeated code and make our code more reusable by using methods and we can simplify our code by using methods so basically there's a lot of reasons to use methods they you know as our programs get more more complicated we actually need to use them so let's take a
look at this program and here I'm just printing out a few lines I print out part 1 and then some equal signs and some stars then I put part 2 and then some equal signs and some stars and I've highlighted a few lines and I'm wondering do you see the similarities in these lines of code we actually have to repeated lines so let's first remember back to methods in Carol when we created methods and Carol it was like teaching the dog a new word and now when we create methods we want to do one specific
thing and make our code easier to understand so let's look at what it looks like to define a method in Java and it looks just like our methods from Carol so we write private void print line break open close parentheses and then in between the curly brackets we actually have the two lines that represent our method so if we look in a more general method you just put the name my method which could be any valid name but will write them in lower camel case and then we'll look at calling a method so here what
you do to call a method is you write print line break open parentheses close parenthesis semicolon so just like calling a method and Carol you can call a method in Java so now let's go into our code editor and look more at this okay so here I have a program called line break which is a console program and i'll run it first to see what it does so basically what you can see it does is it prints our line break and one and our line break part 2 and then part 3 and what we want
to do is we want to write a method we want to avoid that repeated code so we can write private void print line break open closed parentheses enter open curly bracket enter and now what I'll do is I'll move our code from the run method and actually put it in our method and note that everything in our method gets indented once so now what we'll do instead of printing that code is we'll call our method by saying print line break open run throughs close parenthesis semicolon and so if we clear and run that will see
that we get the same program and that will go and replace all those print lines with print line break we're removing all of our repeated code so there you go we replace all of our print lines with print line break and we'll run our code and we can see it does the same thing and additionally it's actually a lot clearer to understand because we can see what it's doing and one other thing is let's say we wanted to change the length of the line breaks you know we want to make them a little bit shorter
now we only need to change that code in one place and then we run the code we'll see it's updated across every line break so we're changing it only once and seeing that effect happen four times otherwise we'd have to change it in four different places so this is our introduction to more general methods in Java methods let us create shortcuts of reusable code