Methods / Functions
•
A method/function is a set of statements
grouped together to perform an operation
•
Useful to break code into methods/functions to
keep code simpler
Method Components
•
Name
•
Modifiers
•
Return type
Method Example
public class HelloWorld
{
public static void main(String args[])
{
//code
}
}
Method Example
public class HelloWorld
{
public static void main(String args[])
{
//code
}
}
Method Example
public class HelloWorld
{
public static void main(String args[])
{
//code
}
}
Method Example
public class HelloWorld
{
public static void main(String args[])
{
//code
}
}
Method Example
public class HelloWorld
{
public static void main(String args[])
{
//code
}
}
Method Example
public class HelloWorld
{
public static void main(String args[])
{
//code
}
}
Method Example
public class HelloWorld
{
public static void main(String args[])
{
//code
}
}
String args[] is the
type and name of
Other Examples
•
private int returnSum(int[] list)
•
void setValue()
Modifiers
•
Access control / Scope
•
Static
•
Final
•
Abstract
Access Control / Scope
•
Will discuss in more detail in future classes
Modifier
Class
Scope
Package
Scope
Subclass
Scope
Package Scope
Outside
public
TRUE
TRUE
TRUE
TRUE
Access Control / Scope
•
public
–
public void go(){
//code here
}
•
protected
–
protected void go(){
//code here
}
•
default
–
void go(){
//code here
}
•
private
Static
•
Will discuss in more detail in future classes
•
Whether or not the method is called by the
class or the object
•
Static methods do not need an instance of the
class to be created to be called
–
ClassName.staticMethod();
Final
•
Will discuss in more detail in future classes
•
Different than a final variable
Abstract
•
Will discuss in more detail in future classes
Synchronized
•
Will discuss in more detail in future classes
•
For multithreading, ensures that the method
Return Types
•
The type of value the function returns
•
Can be any object or primitive, or nothing
–
void – no return value
Return Types
public void returnNothing() {
}
public int returnInt() {
return 1; }
public int returnInt2() {
Parameters
•
Input values the method takes
•
Can be any object or primitive, or none
•
Comma separated list
•
Limit is 255
–
Generally only 3-4 is acceptable
Parameters
//no parameters
void doSomething(){}
//one integer parameter
void doSomething(int x){}
Method Signature
•
All methods within the same class must have a
unique method signature
•
Method signature
Example - SignatureExamples.java
•
SignatureExamples.java
Calling Methods
•
Must call methods with the expected
parameters
•
Methods can be called three ways
Calling Methods
•
Class name
–
Static methods are called with the class name
//calls the floor method of the Math class
Math
.floor(5.2);
Calling Methods
•
Object name
–
Regular methods are called with the object name
Calling Methods
•
No name
–
Methods in the same class can be called without
any name
Example – Method Calling
•
MethodCallExample.java
•
MethodCallExample2.java
Parameter Passing
•
Pass by Value
–
For primitive types
–
Makes a copy of the data and uses the copy
•
Pass by Reference
–
For object types and arrays
Pass by Value
public static int increment(int a) {
a++; return a; }
public static void main(String args[]) {
int a = 5;
Pass by Value
public static int increment(int a) {
a++; return a; }
public static void main(String args[]) {
int a = 5;
int b = increment(a); System.out.println(a); System.out.println(b);
Pass by Value
public static int increment(int a) {
a++; return a; }
public static void main(String args[]) {
int a = 5;
int b = increment(a); System.out.println(a);
Pass by Value
public static int increment(int a) {
a++; return a; }
public static void main(String args[]) {
int a = 5;
int b = increment(a); System.out.println(a); System.out.println(b);
Copy the value of
‘a’ into a new local variable
defined for the function which
Pass by Value
public static int increment(int a) {
a++; return a; }
public static void main(String args[]) {
int a = 5;
int b = increment(a); System.out.println(a);
Pass by Value
public static int increment(int a) {
a++; return a; }
public static void main(String args[]) {
int a = 5;
Pass by Value
public static int increment(int a) {
a++; return a; }
public static void main(String args[]) {
int a = 5;
int b = increment(a); System.out.println(a);
return a
a
6
Pass by Value
public static int increment(int a) {
a++; return a; }
public static void main(String args[]) {
int a = 5;
int b = increment(a); System.out.println(a);
System.out.println(b);
5
a
Store return
value in b
6
Pass by Value
public static int increment(int a) {
a++; return a; }
public static void main(String args[]) {
int a = 5;
int b = increment(a); System.out.println(a);
a
b
Pass by Value
public static int increment(int a) {
a++; return a; }
public static void main(String args[]) {
int a = 5;
Example – PassByValue.java
Pass by Reference
public static int[] increment(int[] a) {
a[0]++; return a; }
public static void main(String args[]) {
int a[] = {5, 10};
int[] b = increment(a);
System.out.println(a[0] + “ ” + a[1]); System.out.println(b[0] + “ ” + b[1]);
Set variable
a[0] = 5
a[1] = 10
Pass by Reference
public static int[] increment(int[] a) {
a[0]++; return a; }
public static void main(String args[]) {
int a[] = {5, 10};
int[] b = increment(a);
System.out.println(a[0] + “ ” + a[1]);
Call increment
with a
Pass by Reference
public static int[] increment(int[] a) {
a[0]++; return a; }
public static void main(String args[]) {
int a[] = {5, 10};
int[] b = increment(a);
System.out.println(a[0] + “ ” + a[1]); System.out.println(b[0] + “ ” + b[1]);
a
A new pointer is made that
points to the SAME memory
Pass by Reference
public static int[] increment(int[] a) {
a[0]++; return a; }
public static void main(String args[]) {
int a[] = {5, 10};
int[] b = increment(a);
System.out.println(a[0] + “ ” + a[1]);
increment the first value
6
, 10
a
Pass by Reference
public static int[] increment(int[] a) {
a[0]++; return a; }
public static void main(String args[]) {
int a[] = {5, 10};
int[] b = increment(a);
System.out.println(a[0] + “ ” + a[1]); System.out.println(b[0] + “ ” + b[1]);
return the array
6, 10
a
Pass by Reference
public static int[] increment(int[] a) {
a[0]++; return a; }
public static void main(String args[]) {
int a[] = {5, 10};
int[] b = increment(a);
System.out.println(a[0] + “ ” + a[1]);
Store the return value in b
6, 10
a
Pass by Reference
public static int[] increment(int[] a) {
a[0]++; return a; }
public static void main(String args[]) {
int a[] = {5, 10};
int[] b = increment(a);
System.out.println(a[0] + “ ” + a[1]); System.out.println(b[0] + “ ” + b[1]);
Print out array a
6, 10
a
Pass by Reference
public static int[] increment(int[] a) {
a[0]++; return a; }
public static void main(String args[]) {
int a[] = {5, 10};
int[] b = increment(a);
System.out.println(a[0] + “ ” + a[1]);
Print out array b
6, 10
a
Example – PassByReference.java
Method Overloading
•
Method with the same name but different
parameters
–
Assumed to have the same purpose
–
Useful when the same task can be done with a
different set of initial parameters
Example - OverloadExample.java
Variable Scope
•
Local Variable = variable defined within the
method
•
Scope starts at declaration
Example - SelectionSort.java
•
Week3
SelectionSort.java
•
What is the scope of unsortedArray?
•
What is the scope of i?
•
What is the scope of j?
•
What is the scope of totalSelectionSort?