Java Method Parameters
Parameters and Arguments
Information can be passed to methods as parameter. Parameters
act as variables inside the method.
Parameters are specified after the method name, inside the
parentheses. You can add as many parameters as you want, just
separate them with a comma.
public class Main {
static void myMethod(String fname) {
System.out.println(fname + " friend");
}
public static void main(String[] args) {
myMethod("Liam");
myMethod("Jenny");
myMethod("Anja");
}
}
When a parameter is passed to the method, it is called an argument. So,
from the example above: fname is a parameter, while Liam, Jenny and
Anja are arguments.
Multiple Parameters
public class Main {
static void myMethod(String fname, int age) {
System.out.println(fname + " is " + age);
}
public static void main(String[] args) {
myMethod(“uma", 5);
myMethod(“naveen", 8);
myMethod("Anjali", 31);
}
}
Note that when you are working with multiple parameters, the
method call must have the same number of arguments as there
are parameters, and the arguments must be passed in the same
order.
Return Values
The void keyword, used in the examples above, indicates that the
method should not return a value. If you want the method to return a
value, you can use a primitive data type (such as int, char, etc.)
instead of void, and use the return keyword inside the method:
public class Main {
static int myMethod(int x) {
return 5 + x;
}
public static void main(String[] args) {
System.out.println(myMethod(3));
}
}
public class Main {
static int myMethod(int x, int y) {
return x + y;
}
public static void main(String[] args) {
System.out.println(myMethod(5, 3));
}
}
Java Method Overloading
With method overloading, multiple methods can have the
same name with different parameters:
int myMethod(int x)
float myMethod(float x)
double myMethod(double x, double y)
public class Main {
static int plusMethodInt(int x, int y) {
return x + y;
}
static double plusMethodDouble(double x, double y)
{
return x + y;
}
public static void main(String[] args) {
int myNum1 = plusMethodInt(8, 5);
double myNum2 = plusMethodDouble(4.3, 6.26);
System.out.println("int: " + myNum1);
System.out.println("double: " + myNum2);
}
}
public class Main {
static int plusMethod(int x, int y) {
return x + y;
}
static double plusMethod(double x, double y) {
return x + y;
}
public static void main(String[] args) {
int myNum1 = plusMethod(8, 5);
double myNum2 = plusMethod(4.3, 6.26);
System.out.println("int: " + myNum1);
System.out.println("double: " + myNum2);
}
}
Modifiers
We divide modifiers into two groups:
• Access Modifiers - controls the access level
• Non-Access Modifiers - do not control access level, but
provides other functionality
Access Modifiers
public: The code is accessible for all classes
private: The code is only accessible within the declared class
protected: The code is accessible in the same package and subclasses.
Non-Access Modifiers
For classes, you can use either final or abstract:
final The class cannot be inherited by other classes
abstract The class cannot be used to create
objects
Modifier Description
final Attributes and methods cannot be overridden/modified
static Attributes and methods belongs to the class, rather than an
object
abstract Can only be used in an abstract class, and can only be used on
methods. The method does not have a body, for example abstract void
run();. The body is provided by the subclass (inherited from).
transient Attributes and methods are skipped when serializing the
object containing them
synchronized Methods can only be accessed by one thread at a time
volatile The value of an attribute is not cached thread-locally, and is
always read from the "main memory"