Method
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
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
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");
}
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
DisplayMessage(5)
Output:
The value is 5
public static void main(String[] args) {
DisplayNumber(4*3-1);
}
Output:
The value is 11
public static void main(String[] args) {
int a=3, b = 4;
DisplayNumber(a*2);
}
Output:
The value is 6
public static void main(String[] args) {
int a=3, b = 4;
`
DisplayNumber(a+b);
}
Output:
The value is 12
Passing Multiple Arguments
This an error:
public static void ShowSum(double num1, num2)
This an correct:
public static void ShowSum(double num1, double num2)
NOTE TO REMEMBER:
ShowSum(2, 4) //Correct
NOTE TO REMEMBER:
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: