Download Java Method Scope - Technology Learner

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Method Scope
Methods in Java have their own scope. Scope is what Java can see. Anything inside of a
method in Java is inside of the method's scope. When Java is executing code inside of a
method, its scope is limited to whatever is inside that method. It cannot look beyond the
method.
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 is an example of a program with variables created inside of each method. Create a new
class MethodScope in your Method and Strings Project and copy the following program.
public class MethodScope {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = 5; double pi = 3.14;
System.out.println(num);
System. out. println(pi);
}
public static void otherMethod() {
double num2 = 6.28;
System.out.println(num2);
There are two variables created in the main method, and there is one variable created in
otherMethod(). When the program is inside the main method, it can print out the values of
num and pi, because they are still within the scope of the Main method.
Add the following highlighted code and try printing the value of num inside of otherMethod.
public static void otherMethod() {
double num2 = 6.28;
System.out.println(num2);
System.out.println(num);
The programs compiles but notice how Eclipse shows you an error. (num); This is because it
is out of scope and there is no way to see the variable num from within another method.
The same is true if you try to print out the value of num2 inside of the main method. Add the
following highlighted code and try printing the value of num2 inside of the Main Method.
Once again Eclipse shows you an error. (num2);
public static void main(String[] args) {
int num = 5; double pi = 3.14;
System.out.println(num);
System. out. println(pi);
System.out.println(num2);
Run the program and notice that Java only prints 5 and 3.14.
Java programs 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 and the program finishes.
It never gets to the other method, so it never sees, creates or prints the variable num2.
Calling Methods
To get Java to go to our other method, we call the methods. Modify your program, add the
following highlighted code, and then run your program. You should see all three outputs.
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = 5; double pi = 3.14;
System.out.println(num);
System. out. println(pi);
otherMethod();
}
public static void otherMethod() {
double num2 = 6.28;
System.out.println(num2);
}
When Java reaches the otherMethod() line of code it execute the first line inside of
otherMethod(). Modify your program again, add the following highlighted code, and then run
your program.
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = 5; double pi = 3.14;
System.out.println(num);
System. out. println(pi);
otherMethod();
System.out.println("Hello world!");
}
public static void otherMethod() {
double num2 = 6.28;
System.out.println(num2);
Java prints out Hello World! last. This is because Java goes inside otherMethod(), then when it
reaches the end of that method, goes back to inside of main, and continues with the next line
of code.
Calling methods allow you go into another method from main, and then inside that other
method, hop into yet another method. Add the following to your code (highlighted) and run
the program.
Follow the line of codes to see how Java executes the code so that you know why Java output
everything in the order that it did.
public class MethodScope {
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = 5; double pi = 3.14;
System.out.println(num);
System. out. println(pi);
otherMethod();
System.out.println("Hello world!");
}
public static void otherMethod() {
double num2 = 6.28;
method2();
System.out.println(num2);
}
public static void method2() {
System. out.println(3.14159);
}
}
Passing Parameters
Passing parameters 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.
To pass variables, all you have to do is to put the variables inside of the method call
parenthesis.
Add the following and run your program. You will get an error. This is because the method
needs to allow those two parameters to be passed on to it.
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = 5; double pi = 3.14;
System.out.println(num);
System. out. println(pi);
otherMethod(num, pi);
System.out.println("Hello world!");
}
You need to set up otherMethod to accept the variables num and pi by putting the list of
accepted variables inside of the parenthesis. These parameters are really just copies of the
original variables. Add the following:
public static void otherMethod(int num, double pi) {
double num2 = 6.28;
method2();
System.out.println(num2);
Once the variables are passed to the method, you are free to use them.
public static void otherMethod(int num, double pi) {
double num2 = 6.28;
method2();
System.out.println(num + num2 + pi);
//Create a comment section and write down the output of the code.