Methods In Java Revisited

Introduction

Methods in Java are used to make code easier to read and to make sure that code is not duplicated. Code that needs to be run over and over again is placed in a method, which is just a sequence of instructions all placed together in one spot. This is basically what methods are, but it's important to make sure we've got the details nailed down. In other words, you need to know how methods in Java truly work so that you know how to best use them.

I remember having a lot of trouble understanding how to use methods in Java when I was first learning, and it's because they skipped all of the details I needed to know. We're gonna make sure that doesn't happen by filling up any holes you might have in your knowledge of methods.

Method Scope

Methods in Java have their own scope. What is method scope? Scope is what you can see. In this case, it's what Java can see.

Let's use eye scope as an example. Anything you can see would be within your eye scope. Anything beyond that is out of your sight, and thus out of your scope. If you live in the United States, you can't walk out of your house and see China in the distance. China is said to be out of your eye scope. There's simply no way you're going to see it.

Anything inside of a method in Java is inside of the method's scope. When Java is executing code inside of a method, it's scope is limited to whatever is inside that method. It cannot look beyond the method. So what exactly do I mean?

Variables can be created anywhere in your program. You typically create variables inside of methods, such as the main method, or some other Java method that you create. Here's an example of a program with two methods, and it has variables being created inside of each method. We're going to see which variables a program can see depending on which part of the code your program is executing.

There are variables created in the main method, and there is a variable created in otherMethod(), another Java method. When the program is inside the main method, it can print out the values of num and pi, becaise they are still within the scope of the method. Try printing out the value of num inside of otherMethod().

Notice how in my example, my compiler in Eclipse is showing me that I have an error when I try to print out the value of num when I'm inside otherMethod(). This is because there is no way to see the variable num from within another method besides the main. So, it is said to be out of scope. The same is true if I try to print out the value of num2 inside of the main method.

This didn't work for the exact same reason. Inside the main method, Java can't see any variables created in other methods.

Get rid of/fix the lines of code that have errors in them, and run the program.

You'll see that only two of the println's actually worked. What happened to the third one? Remember, Java programs start at the top of the main method. This means that Java starts executing code beginning with the first line it finds inside of the main method. Java creates the num variable, the pi variable, prints out the num variable, prints out the pi variable, and then reaches the end of the main. When that happens, the program finishes. It never gets to the other method, so it never printed out the value of num2. It never even created the num2 variable!

Calling Methods

So how are we going to get Java to go to our other method?

Simple, we have to call the method. Calling methods in Java is much easier shown than explained, so here is how I call otherMethod().

Now if you run the program, you will see all three outputs. What happens is when Java reaches the otherMethod() line of code, that's a signal for it to go into that method. So then, the program jumps to the first line inside of otherMethod(), which happens to be when you create variable num2. When Java reaches the end of that method, it goes back to main where it came from and continues to execute code. I'll add another print statement inside of main to illustrate.

If you run the program now, you'll see that it prints out Hello World! last. This is because Java goes inside otherMethod(), then when it reaches the end of that method, goes back to where it last was inside of main, and continues with the next line of code.

I recommend creating your own methods and experimenting with how Java hops around from one method to another.

It's possible to go into another method from main, and then inside that other method, hop into yet another method.

Code up that example yourself and check out your results. Trace how Java executes the code with your finger to prove to yourself that you can follow along with the code and that you know why Java output everything in the order that it did.

Passing Parameters

Creating methods in Java seems useless if we stick to the examples I've shown and you've hopefully followed. What if another method needs to use the same variables that were created in the main method?

This can be done via passing those variables as parameters. What this does is it makes a copy of the variables you want to send to the other method for it to use. They are copied temporarily until the method finishes, then those temporary variables go away.

Note: All methods in Java work this way, unlike in other languages. For example, C++ has a way to pass a reference to the actual variable. Java has no such thing.

To pass variables, all you have to do is to put the variables inside of the method call parenthesis. Here's an example of how I would pass num and pi to otherMethod().

You'll see that I put num and pi inside the parenthesis when I call the otherMethod() method. However, this is only half of the story. If you're following along with your code, you'll notice that you get an error. This is because the method needs to allow those two parameters to be passed to it.

It's not enough to say okay, this method will accept two variables. You need to tell the method exactly what variables are going to be given to it. Here is how I would set up otherMethod to accept the variables num and pi.

Note how I put the list of accepted variables inside of the parenthesis. The method accepts two variables, an int variable and a double variable. I also had to name the variables. Yes, the names of these variables are the same as the ones in the main method but they do not have to be. That's right, I could have called those two variables variable1 and variable2, and it wouldn't make a difference. This is because these parameters are really just copies of the original variables. Remember, otherMethod() cannot see the variables created in the main method because they are out of scope.

The order in which you pass the parameters is important, too. I put the int variable first, then the double, so that's how you have to pass the values. In the main, you cannot put pi first and then num, because otherMethod() takes first an int and then a double. All methods in Java, like in C++ and other object-oriented languages, enforce the ordering of passed parameters.

Once you've passed your variables, you're free to use them.

You now should have a much better understanding about what methods in Java are and how to use them. You should also now know how Java executes code, especially when it starts to skip around into different methods. I know for a fact that a lot of people struggle with this concept, so it's important to get it right, If you're stuck here, it's ok. Keep playing around with methods and re-read and follow the examples in this tutorial. You may also contact me by filling out the questions form below. I'll be glad to help in any way I can.

In the next tutorial you'll learn all about the logical operators Java has. They'll be a big help in making your code smaller and will allow you to do more powerful stuff.

If you have any questions, comments, or concerns, feel free to contact us.

Custom Search