declare

Methods in Java Tutorial



Sharing buttons:

methods in Java can be pretty tricky but

I promise you if you watch this video

you'll have a fully working program

using methods what's up it's alex back

again helping you learn java on this

channel I make Java tutorials just like

this all the time so if that's something

you're into then consider subscribing a

method is just a chunk of code that you

run with one line I'll show you that

right now first let's just set up this

project by creating a new Java project

we'll call it methods and we will throw

our Java class in here by right-clicking

source going to new class we'll call it

methods again take this first check mark

and hit finish let's say we have a

little calculator program here we have

an integer called a that is equal to 5

and we have an integer called B that's

equal to like 10 if we wanted to

multiply these two numbers a times B you

would print it out to the screen a times

B you save it and run it you'll get 50

if we wanted to do this exact same thing

but for 2 different numbers well we

could copy and paste it change these

variables to something different and

then change the numbers and if we save

and run that we get 6 if we want to do

this a third time you know we'd have to

do we'd have to copy and paste this

again and this can be really annoying

but let's say we did want to do this 3

times like that and this is where

methods come in we can call these 9

lines of code with just 3 calls to our

method to make a method you type public

static void so name your method we will

call this multiply but some parentheses

and some curly braces you'll notice that

this is almost the exact same format as

this right here public static void main

that's why this is called the main

method it's a special chunk of code

that's run when you hit the Run button

to pass values into your method

you would type them in here in these

parentheses we'll say a and B and then

what we do want to do well we want to

print out the multiplication of a and B

like that so we can just replace these

lines with our method so we have a is 5

and B is 10 we will just say multiply 5

and 10 if we save this and run it we

will still get the 50 because our

multiply method does exactly the code

that we want it so we'll replace this we

have 2 and 3 and then we'll multiply 6

and 8 and this looks so much better than

what we had before but it does the exact

same thing if we change this I wanted to

do five times a hundred we could easily

change it right then and there so let's

keep practicing let's say we want to

welcome our guests to our calculator

program we could do it like that or we

could practice methods and we would do

public static void

welcome welcome will be the name of our

method it can be named anything and we

will stick our welcome message in the

Welcome method and just call it like

that and then this code will go into

welcome and do what's ever in there

let's save it run it we get that method

call let's do one more method example

and we'll call it divide so to start a

method we just do some keywords not sure

exactly what it means yet but we know

that this will make a method public

static void we'll call it divided and

we'll throw in our two numbers we want

to divide and we'll put that code inside

these curly braces so we print out a

divided by B just like that's and then

we'll do some division we'll say divide

20 and 5

100/10 66 by 11 save that run it and we

see everything is working properly this

makes it really easy to reuse code

whenever you want make sure you

subscribe if you haven't already so you

can get new videos from me the day we

come out if this is helpful smash that

like button and share it if you think it

might help someone you know you could be

anywhere in the world but here with me

and I appreciate it catch