Java
Programming
JAVA METHODS
Java Methods
• A method is a program module that contains a series of statements that carry
out a task.
• A method is just a chunk of code that does a particular job.
The Structure of a Method
Return Type
Specifier Method Name Parameter
Method Header public static int getAverage(int average) {
Int a, b, c;
a=15; b=30; c=10;
Method Body
average = (a + b + c) / 3 ;
return average;
}
• An optional access specifier—The access specifier for a Java method can
be any of the following modifiers: public, private, protected, or, if left
unspecified, package.
• The return type for the method—A method might return a value, or send
it back, to its calling method. If so, the method’s return type is the data
type of the returned data. A method that returns no data has a return
type of void.
• The method name—The name can be any legal identifier. That is, it must be
one word with no embedded spaces, and cannot be a Java keyword.
• A set of parentheses—The parentheses optionally might contain data to be
sent to the method (parameter).
• The method body is where the code goes.
Example1:
public class TestMethods {
public static void main(String[] args) {
printHeading();
}
public static void printHeading() {
System.out.println(“Saint Vincent College of Cabuyao");
System.out.println(“BSIT");
}
}
Note:
public means that the method can be seen outside of the class
static means that you don't have to create a new object using instantiation
void means it doesn't return a value - it just gets on with it.
Example2:
public class TestMethods {
public static void main(String[] args) {
TestMethods test = new TestMethods();
test.printHeading();
}
void printHeading() {
System.out.println(“Saint Vincent School of Cabuyao");
System.out.println(“BSIT");
}
}
Example3:
public class TestMethods {
public static void main(String[] args) {
System.out.println("The total for 15 and 14 is: " + addInteger());
}
public static int addInteger() {
int a = 15;
int b = 14;
return a + b;
}
}
You do it
Create a java class that will find the sum, difference, product,
and quotient of 15 and 3. Apply the java method.
Output:
The sum of 15 and 3 is 18
The difference of 15 and 3 is 12
The product of 15 and 3 is 45
The quotient of 15 and 3 is 5
Two Truths and a lie
1. A class header contains an optional access specifier, the
keyword class, and an identifier.
2. When you instantiate objects, each has its own copy of
each static data field in the class.
3. Most fields in a class are private, and most methods are
public.
Passing Values to your Java Methods
• You can pass values to your methods so that something can be done with this
value. That value goes between the round brackets of the method.
• Data items you use in a call to a method are called arguments.
• When the method receives the data items, they are called parameters.
public class mySample {
public static void main(String[] args) {
int num1 = 25;
int num2 = 38;
Arguments
int largest;
largest = getMax(num1,num2);
System.out.println("The largest number is: " + largest);
}
Parameters
public static int getMax(int a, int b) {
int max;
max = Math.max(a, b);
return max;
}
}
Overloading a Method
• Writing multiple methods with the same name but with different parameter
lists.
Example:
int total() {
int a_value = 10 + 10;
return a_value;
}
int total(int a_number) {
int a_value = a_number + 10;
return a_value;
}
Two Truths and a lie
1. When you overload Java methods, you write multiple
methods with a shared name.
2. When you overload Java methods, the methods are
called using different arguments.
3. Instead of overloading methods, it is preferable to write
methods with unique identifiers.