Lesson Plan - java methods and scope of variables
Lesson Plan - java methods and scope of variables
scope of variables
Pre-Requisites
Java classes and object
Basics of methods
Variables are the most versatile part of the codes because they not only enable us to take values but also
share it all across the program/application. It is therefore, extremely important to know some rules about the
legality of access of variables across the program. Yes, you cannot use all kinds of variables freely in a large
scale program.
To give you an insight to these rules of accessing a variable, we will start the lesson with the concept of ‘scope’.
Example:
int score;
Example:
void game()
int score;
A variable declared inside a pair of brackets “{” and “}” in a method has scope within the brackets only.
Example:
{ for(int score=0;score<100;score++){
// brackets
System.out.println(score);
// System.out.println(score);
Note : Within the same scope, no two variables can be declared with the same name. However, it is possible for
two variables to have the same name if they are declared in different scope.
Example: .
int p = 50;
double p = 60.70;
System.out.println(p);
Before moving ahead to the next concept, let us first understand an important concept parameters that will be
used to pass the variables in methods.
There are two types of parameters one is Formal parameters and the second is Actual parameters.
Formal parameters: These are the parameters that are defined during function/method definition.
Actual parameters: These are the parameters which are passed during the function/method call in other
Function.
Example:
import java.io.*;
class Main {
return p-q;
int p=89;
int q=9;
System.out.println(diff(p,q));
Output: 80
This parameter passing in methods is done in two ways, let us look at each of them.
Note: Java is officially always pass-by-value i.e when any variable is passed to a method in Java, the value of
the variable on the stack is copied into a new variable inside the new method.
int a = 40;
changeValue(a);
...
import java.util.Scanner;
int a, b, ans;
a = sc.nextInt();
b = sc.nextInt();
return ans;
In the above example, we are passing two values a and b entered by the user in a function add which is
returning the sum of two numbers i.e a and b.
n1--;
n2 = n2 - 2;
int p = 26;
int q = 13;
decrease(p,q);
Ans:
25: 11
26: 13
Explanation:
For the above code, changes in the values of n1 and n2 are not reflected to p and q because n1 and n2 are
formal parameters and are local to function decrement so any changes in their values here won’t affect
variables p and q inside main.
p = p * 2;
int p = 24;
makeTwice(p);
System.out.println(p);
Ans : 24
Explanation:
For the above code, changes in the values of p is not reflected in main function
int q = p;
q = q -100;
int p = 890;
temp(p);
System.out.println(q);
Explanation: Because in the main function there is no variable having name q,variable q has scope in temp
function block only.
That is all for this lesson. See you in the next class !