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

Creating Method: Eelo University Faculty of Computer Science Class: DIT

The document discusses Java methods in three paragraphs: 1) It defines what a Java method is and provides an example of the System.out.println() method. 2) It explains how to create methods with or without return values, invoke methods with or without parameters, and apply method abstraction in program design. 3) It discusses how to call methods, including methods that return values and methods specified as void, and provides examples to demonstrate defining and calling methods.

Uploaded by

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

Creating Method: Eelo University Faculty of Computer Science Class: DIT

The document discusses Java methods in three paragraphs: 1) It defines what a Java method is and provides an example of the System.out.println() method. 2) It explains how to create methods with or without return values, invoke methods with or without parameters, and apply method abstraction in program design. 3) It discusses how to call methods, including methods that return values and methods specified as void, and provides examples to demonstrate defining and calling methods.

Uploaded by

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

EELO UNIVERSITY

FACULTY OF COMPUTER SCIENCE


Class: DIT
Course: OOP in Java

Chapter Five: Java - Methods

A Java method is a collection of statements that are grouped together to


perform an operation. When you call the System.out.println() method, for
example, the system actually executes several statements in order to display
a message on the console.

Now you will learn how to create your own methods with or without return
values, invoke a method with or without parameters, and apply method
abstraction in the program design.

Creating Method
Considering the following example to explain the syntax of a method −

Syntax
public static int methodName(int a, int b) {
// body
}

Here,

 public static − modifier

 int − return type

 methodName − name of the method

 a, b − formal parameters

 int a, int b − list of parameters


Method definition consists of a method header and a method body. The same
is shown in the following syntax −

Syntax
modifier returnType nameOfMethod (Parameter List) {
// method body
}

The syntax shown above includes −

 modifier − It defines the access type of the method and it is optional to use.

 returnType − Method may return a value.

 nameOfMethod − This is the method name. The method signature consists of


the method name and the parameter list.

 Parameter List − The list of parameters, it is the type, order, and number of
parameters of a method. These are optional, method may contain zero
parameters.

 method body − The method body defines what the method does with the
statements.

Example

Here is the source code of the above defined method called min(). This
method takes two parameters num1 and num2 and returns the maximum
between the two −

/** the snippet returns the minimum between two numbers */

public static Double add(Double a, Double b)

return a + b;

public static Double subt(Double a, Double b)

return a - b;

Method Calling
For using a method, it should be called. There are two ways in which a method
is called i.e., method returns a value or returning nothing (no return value).

The process of method calling is simple. When a program invokes a method,


the program control gets transferred to the called method. This called method
then returns control to the caller in two conditions, when −

 the return statement is executed.

 it reaches the method ending closing brace.

The methods returning void is considered as call to a statement. Lets consider


an example −

System.out.println("This is tutorialspoint.com!");

The method returning value can be understood by the following example −

int result = sum(6, 9);

Following is the example to demonstrate how to define a method and how to


call it −

Example
Live Demo

public class ExampleAddandSubtNumbers {

public static void main(String[] args) {

int result = Add(120, 100);

result = Sub(140, 100);

Public static int (int a, int b)

Return a + b

Public static int (int a, int b)

{
Return a - b

This will produce the following result −

Output
Minimum value = 6

The void Keyword


The void keyword allows us to create methods which do not return a value.
Here, in the following example we're considering a void
method methodRankPoints. This method is a void method, which does not
return any value. Call to a void method must be a statement
i.e. methodRankPoints(255.7);. It is a Java statement which ends with a
semicolon as shown in the following example.

Example
Live Demo

public class ExampleVoid {

public static void main(String[] args) {

methodRankPoints(255.7);

public static void methodRankPoints(double points) {

if (points >= 202.5) {

System.out.println("Rank:A1");

}else if (points >= 122.4) {

System.out.println("Rank:A2");

}else {

System.out.println("Rank:A3");

}
}

This will produce the following result −

Output
Rank:A1

Passing Parameters by Value


While working under calling process, arguments is to be passed. These should
be in the same order as their respective parameters in the method
specification. Parameters can be passed by value or by reference.

Passing Parameters by Value means calling a method with a parameter.


Through this, the argument value is passed to the parameter.

Example

The following program shows an example of passing parameter by value. The


values of the arguments remains the same even after the method invocation.

You might also like