0% found this document useful (0 votes)
13 views

Method

Uploaded by

kimtrm34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Method

Uploaded by

kimtrm34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Using Methods in Java

A divide and conquer algorithm is a strategy of solving a


large problem by

Breaking the problem into


smaller sub-problems

Solving the sub-problems

Combining them to get


the desired output.
What is a method?


A method is a collection of statements that are grouped
together to perform an operation.


It is a named, independent section of Java code
that performs a specific task and optionally returns a
value to the calling program.
Syntax

[accesskeyword] static returntype methodname


(parameter) {
}

Access keyword
• The access keyword can be public, private, or protected.
• The special form of access called package is the default
if no access modifier is declared
Syntax

[accesskeyword] [static] returntype methodname


(parameter) {
}

static
• The keyword static implies that only one instance of the
member exists for a class.
• Static variables are used for defining constants because
their values can be retrieved by invoking the class
without creating an instance of it.
Syntax
[accesskeyword] [static] returntype methodname
(parameter) {
}

return type
• The return type can be any Java primitive data type,
an object, or void. Void denotes hat the method has
no return value.
• The return type indicates the type of value returned
or reported back to the calling method
Syntax
[accesskeyword] [static] returntype methodname
(parameter) {
}

method name
• The method name follows the same naming rules as
variables and will generally begin with a lowercase
letter.
• Because methods and variables are both contained
inside classes, they are called class members.
Syntax
[accesskeyword] [static] returntype methodname
(parameter) {
}
parameter
• It receives the value being passed when a function is
called
• The parameter list is a comma-separated
• The parameter is a variable inside the method, and the
parameter name is the name used for reference.
What are the example of
access keyword?
return method Formal
modifier
type name parameter

method
header

method
parameter
body
list
public static void main(String[] args) {
System.out.println("Main Methods");
DisplayMessage();
System.out.println("Back to Main
Methods");
}

static void DisplayMessage(){


System.out.println("Programming is great
fun!");
}
public static void main(String[] args) {
System.out.println("Main Methods");
DisplayMessage();
System.out.println("Back to Main
Methods");
}
static void DisplayMessage(){
System.out.println("Programming is great
fun!");
}

Output
Main Methods
Programming is great fun!
Back to Main Methods
public static void main(String[] args) {
DisplayNumbers();
DisplayMessage();
} static void DisplayNumbers(){

System.out.println("One");

System.out.println("Two");

System.out.println("Three");
static void DisplayMessage(){ }
System.out.println("Programming is great
fun!");
}
public static void main(String[] args) {
DisplayNumbers();
DisplayMessage();
}
static void DisplayMessage(){
System.out.println("Programming is great
fun!");
}
static void DisplayNumbers(){
One
System.out.println("One"); Two
Three
System.out.println("Two"); Programming is great fun
System.out.println("Three");
Passing Arguments to a Method

 A method may be written so it accepts arguments. Data can then


be passed into the method when it is called.
 Values that are sent into a method are called arguments. You’re
already familiar with how to use arguments in a method call.
 A parameter variable, sometimes simply referred to as a
parameter, is a special variable that holds a value being passed
into a method.
Here is an example of a call to the displayValue method,
passing 5 as an argument:

DisplayMessage(5)

static void DisplayMessage(int num){


System.out.println("The value is" + num);
}
public static void main(String[] args) {
DisplayNumber(5);
}

static void DisplayMessage(int num){


System.out.println("The value is" +
num);
}

Output:
The value is 5
public static void main(String[] args) {
DisplayNumber(4*3-1);
}

static void DisplayMessage(int num){


System.out.println("The value is" +
num);
}

Output:
The value is 11
public static void main(String[] args) {
int a=3, b = 4;
DisplayNumber(a*2);
}

static void DisplayMessage(int num){


System.out.println("The value is" +
num);
}

Output:
The value is 6
public static void main(String[] args) {
int a=3, b = 4;
`
DisplayNumber(a+b);
}

static void DisplayMessage(int num){


System.out.println("The value is" +
num);
}

Output:
The value is 12
Passing Multiple Arguments

 It is possible to declared inside the parentheses in


the method header.
 This is often referred to as a parameter list.
 Notice that a comma separates the declarations .
ComputeArea(7, 4);

static void ComputeArea(int length, int width){


int area;
area = length * width;
System.out.println("The area is" + area);
}
NOTE TO REMEMBER:

Each parameter variable in a parameter list must


have a data type listed before its name.

This an error:
public static void ShowSum(double num1, num2)

This an correct:
public static void ShowSum(double num1, double num2)
NOTE TO REMEMBER:

The number of arguments must match in the number of


parameter

static void ShowSum(int a, int b)

ShowSum(20) //Error ShowSum(5, 2, 9) // Error

ShowSum(2, 4) //Correct
NOTE TO REMEMBER:

The datatype must match when you call the method.

static void ShowSum(int a, int b)

ShowSum(20, 30); //Correct ShowSum(5, 2.3); //Error

ShowSum(2.4, 4); //Error ShowSum(2.0, 3.4); //Error


Local Variables

 A local variable is declared inside a method and is


not accessible to statements outside the method.
 Different methods can have local variables with
the same names because the methods cannot see
each other’s local variables.
 Statements outside a method cannot access that
method’s local variables.
public static void main(String[] args)
Local
{
Variable:
int x, y;
args, x, y
}

static void Show(int a, int b){ Local


int sum; Variable:
} sum, a, b

static void Compute(double d){ Local


int prod, sq; Variable:
d,prod, sq
}
NOTE TO REMEMBER:

Local variable is not accessible in


other method. Trying to access the
local variable in other method is an
error.
static void Display() {
int x=20; The sum variable is
ShowNum(4, 2); not accessible in
Display since it is a
System.out.println(sum);
local variable of
} ComputeSum

static void ComputeSum(int a, int b) {


int sum = a + b + x;
} The x variable is not
accessible here since it a
local variable of Display
NOTE TO REMEMBER:

Local variables are not automatically


initialized with a default value. They must
be given a value before they can be
used. If you attempt to use a local
variable before it has been given a
value, a compiler error will result
public static void myMethod()
{
int x;
System.out.println(x);
}
Error
Local variable must
be initialized
public static void main(String[] args) {
int x= 20;
myMethod();
}

public static void myMethod()


{
int x;
System.out.println(x);
}
NOTE TO REMEMBER:

The value of the local value is not affected


when the value of variable is changed in other
method.
static void Main(){
int x=10;
System.out.println("Before X value:" + x);
ChangeValue(x);
System.out.println("After X value:" + x);
} Before X value:10
Inside X value:20
After X value:10

static void ChangeValue(int x){


int x=20;
System.out.println("Inside X value:" + x);
}
Returning a Value from a Method
 A method may send a value back to the
statement that called the method.
 Data may also be returned from a method,
back to the statement that called it.
 Methods that return a value are appropriately
known as value-returning methods
Defining a Value-Returning Method
 When you are writing a value-returning method, you must decide
what type of value the method will return.
 A value-returning method will use int, double, boolean, or any
other valid data type in its header.
 Here is an example of a method that returns an int value:

int total = GetSum(50, 200)


250

public static int GetSum(int num1, int num2) {


int result; 50 200
result = num1 + num2;
return result;
} 250
public static int GetSum(int num1, int num2) {
total = GetLarge(5, 6) + Multiply(3, 4, 2);
} -1 24

5 6
static int GetLarge(int first, int second){
if (first > second)
return first + second;
3 4 2
else
return first - second; -1 static int Multiply(int x, int y, int z){
} int product = x * y * z;
return product; 24
}
NOTE TO REMEMBER:

The void value must not return any


value. Void method can use the
return statement but it doesn’t not
have any value
static void ComputeArea(int num1, int num2){ Error since void
int sum = num1+num2; method cannot
return sum ; return any value
}

static void ComputeArea(int num1, int num2){


The return can be
int sum = num1+num2; use in used but it it
return; doesn’t not have
} any value
NOTE TO REMEMBER:

The non void value must return any


value otherwise it would be a
compilation error
static void ComputeArea(int num1, int num2){ Error since void
int sum = num1+num2; method cannot
return sum ; return any value
}

static void ComputeArea(int num1, int num2){


The return can be
int sum = num1+num2; use in used but it it
return; doesn’t not have
} any value
NOTE TO REMEMBER:

The non void method must return


any value otherwise it would be a
compilation error
static int ComputeArea(int num1, int num2){
int sum = num1+num2;
}

This will result to error


since non void methods
must return a value
NOTE TO REMEMBER:

Putting a statement after the


return statement is an error. The
statement after the return
statement is a dead code or
unreachable statement
static int ComputeArea(int num1, int num2){
int sum = num1+num2;
return sum;
System.out.println ("The sum is " +
sum);
}

This is an error since


statement after the
return are dead code or
unreachable statements

You might also like