Chapter-1 (1)
Chapter-1 (1)
Methods
Opening
Problem
• Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to
45, respectively.
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
System.out.println("Sum from 1 to 10 is " + sum);
sum = 0;
for (int i = 20; i <= 30; i++)
sum += i;
System.out.println("Sum from 20 to 30 is " + sum);
sum = 0;
for (int i = 35; i <= 45; i++)
sum += i;
System.out.println("Sum from 35 to 45 is " + sum);
Solutio
n
• The main advantage of a static method is that we can call it without creating an
object.
• It can access static data members and also change the value of it.
• It is used to create an instance method.
• It is invoked by using the class name.
• The best example of a static method is the main() method.
Calling
Methods
CAUTIO
N
• A return statement is required for a value-returning method.
• The method shown below in (a) is logically correct, but it has a
compilation error
because the Java compiler thinks it possible that this method does not
return any value.
To fix this problem, delete if (n < 0) in (a), so that the compiler will see a return
statement to be reached regardless of how the if statement is evaluated.
Reuse Methods from Other
Classes
• NOTE: One of the benefits of methods is for
reuse.
• The max method can be invoked from any class
besides TestMax.
• If you create a new class Test, you can invoke the max
method using
• ClassName.methodName
• (e.g.: TestMax.max).
Call
Stacks
“Each time a method is invoked, the system stores parameters
and variables in an area of memory (RAM) known as a stack,
which stores elements in last-in, first-out fashion."
When a method calls another method, the caller’s stack space is
kept intact, and new space is created to handle the new method
call.
When a method finishes its work and returns to its caller, its
associated space is released.
last-in first-out (LIFO)
Call
Stacks
void Method
Example
• This type of method does not return a value. The method
performs some actions.
Passing
Parameters
public static void nPrintln(String message, int n) {
for (int i = 0; i < n; i++)
System.out.println(message);
}
19
public class Calculator { public class Calculator {
public static void main(String[] args)
{
int a=6; public static void Add(int a, int b) {
int b=2; System.out.println(a+b);
Add(a,b); }
Subtract(a,b);
Multiplication(a,b); public static void Subtract(int a, int b) {
Division(a,b); System.out.println(a-b);
} }
public static void Add(int a, int b) {
System.out.println(a+b); public static void Multiplication(int a, int b) {
} System.out.println(a*b);
public static void Subtract(int a, int b) { }
System.out.println(a-b);
} public static void Division(int a, int b) {
public static void Multiplication(int a, int b) { System.out.println(a/b);
System.out.println(a*b); }
} Gives the
public static void Division(int a, int b) { Same output public static void main(String[] args) {
System.out.println(a/b); int a=6;
} int b=2;
}
Add(a,b);
Subtract(a,b);
Multiplication(a,b);
Division(a,b);
}
Overloading
Methods
Overloading methods enables you to define the methods with the same name if
their signatures are different.
Overloading the max Method
}
Ambiguous
Invocation
• Sometimes there may be two or more possible matches for an
invocation of a
method, but the compiler cannot determine the most specific
match.
• This is referred to as ambiguous invocation.
• Ambiguous invocation
public is a compilation
class AmbiguousOverloading { error.
public static void main(String[] args) {
System.out.println(max(1, 2));
}
public static double max(int num1, double num2){
if (num1 > num2)
return num1;
else
return
num2;
}
public static double max(double num1, int num2){
if (num1 > num2)
return num1;
else
return
num2;
}
}
The End of Chapter-1