Lecture 14 - Methods Static VS. Non-Static, Defining and Calling Method, Argument Passing
Lecture 14 - Methods Static VS. Non-Static, Defining and Calling Method, Argument Passing
Opening Problem
• Find the sum of integers from 1 to 10, from 20
to 30, and from 35 to 45, respectively.
Problem
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);
Problem
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);
Solution
public static int sum(int i1, int i2) {
int sum = 0;
for (int i = i1; i <= i2; i++)
sum += i;
return sum;
}
• Pre-defined Methods
– Already defined in the Java class libraries known
as standard library method or built-in method
– println(), Math.pow(), input.nextInt(), etc.
• User-defined Methods
– The method written by the user or programmer
– Modified as per requirement
Defining a Method
• Syntax
modifier returnValueType methodName(list of
parameters) {
// Method body;
}
Defining a Method
• Access Specifier:
– It specifies the visibility of the method. Java provides
four types of access specifier:
– Public: The method is accessible by all classes
– Private: The method is accessible only in the classes in
which it is defined
– Protected: The method is accessible within the same
package or subclasses in a different package
– Default: When we do not use any access specifier in the
method declaration, Java uses default access specifier by
default. It is visible only from the same package only.
Method Signature
static:
Static methods are the method
which invokes without creating
the objects, so we do not need
any object to call the main()
method.
String args[]:
main(): The main() method also
void: It is a default signature which is accepts some data from the
Void keyword acknowledges the predefined in the JVM. It is called user. It accepts a group of
compiler that main() method by JVM to execute a program line strings, which is called a string
does not return any value. by line and end the execution after array. It is used to hold the
completion of this method. We can command line arguments in
also overload the main() method. the form of string values.
Java main() method
• What happens if the main() method is written without String
args[]?
– The program will compile, but not run, because JVM will not recognize
the main() method
– JVM always looks for the main() method with a string type array as a
parameter. public static void main(String[] args){
System.out.println("Hello");
• Execution Process }
static //static block
1. First, JVM executes the static block {
System.out.println("Static block");
2. Then it executes static methods }
3. Then it creates the object needed by the program
4. Finally, it executes the instance methods
Different way to write main()
• public static void main(String args[])
• static public void main(String args[])
• static public void main(String[] x)
• static public void main(String []x)
• static public void main(String...args)
Calling a Method
• Calling a method executes the code in
the method
• Method definition: indicates what the
method does
• How to call a method?
1. Value returning method: a call to the
method is treated as a value
int larger = max(3, 4);
or System.out.println(max(3, 4));
int j = 2;
Activation record for main Activation record for main
int k = max(i, j); method method
System.out.println("The max
of " + i + " and " + j + " is " + k); k: k:
} j:2 j:2
i:5 i:5
/* Returns the maximum of two numbers
*/
public static int max(int num1, int
num2){
int result;
if(num1 > num2)
Activation record for main
result = num1; method
else
result = num2; k:5
j:2 Stack is empty
return result; i:5
}
Void method
• A void method does not
return a value.
• No return statement is
used; ending brace
returns the control to
the caller method
23
Void method with return
Passing arguments by values
• The arguments are passed by value to parameters
when invoking a method (pass-by-value)
– If the argument is a variable, the value of the variable is
passed to the parameter
• Parameter order association
– Arguments must be in the same order as their respective
parameters in the method signature
• Example:
• nPrintln(“Hi”, 3);
• nPrintln(3, “Hi”);
• nPrintln(“Hi”, ‘3’);
Pass-by-value (example 1)
Pass-by-value (example 2)
System.out.println(x);
System.out.println(y);
}
public static void
Method1(){
int x = 1;
int y = 10;
System.out.println(y);
System.out.println(z);
}
public static void
Method2(){
int x = 13;
int y = 12;
Scope of variables
A common mistake
Summary
• Method definition & calling
• Value returning method & void method
• The call stack
• Transfer of control
• Pass-by-value (argument passing)
• Method overloading
• The scope of variables