This section contains the detail about the Methods in Java.
Methods in Java
A method is a set of statements gathered together to carry out some operation.
Making a Method :
Given below the general syntax of a method :
modifier returnType methodName(parameters) {
//body;
}
Example :
This example returns the number which has maximum value :
public static int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Method Calling :
The methods which returns value can be used to pass value to variables :
If the method return type is void then it van be used as statement. For example, the method println returns void. The following call is a statement :
System.out.println("Welcome to Devmanuals!");
Example :
public class TestMethod {
public static void main(String[] args) {
int a = 5;
int b = 2;
int c = max(a, b);
System.out.println("The maximum between " + a +
" and " + b + " is " + c);
}
/** Return the max between two numbers */
public static int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Output :
|
C:\Program Files\Java\jdk1.6.0_18\bin>javac TestMethod .java C:\Program Files\Java\jdk1.6.0_18\bin>java TestMethod The maximum between 5 and 2 is 5 |
Method Overloading :
In java, it is possible that two methods have same name, but their parameter declaration must be different . This is called method overloading.
Example
public class DemoOverload {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a:" + a);
}
// Overload test for two integer parameters
void test(int a, int b) {
System.out.println("a and b :" + a + " " + b);
}
}
class overload {
public static void main(String args[]) {
DemoOverload d = new DemoOverload();
d.test();
d.test(10);
d.test(10, 20);
}
}
Output :
|
C:\Program Files\Java\jdk1.6.0_18\bin>javac Overload.java C:\Program Files\Java\jdk1.6.0_18\bin>java Overload No parameters a:10 a and b :10 20 |
Constructor overloading :
The constructor can be overloaded in java, which is helpful in initializing variables and methods(constructors).
Example :
public class ConstructorOverloading {
public static void main(String args[]) {
Rectangle rectangle1 = new Rectangle(2, 4);
int areaInFirstConstructor = rectangle1.first();
System.out.println(" The area of a rectangle in first constructor is :"+ areaInFirstConstructor);
Rectangle rectangle2 = new Rectangle(5);
int areaInSecondConstructor = rectangle2.second();
System.out.println(" The area of a rectangle in first constructor is :"+ areaInSecondConstructor);
Rectangle rectangle3 = new Rectangle(2.0f);
float areaInThirdConstructor = rectangle3.third();
System.out.println(" The area of a rectangle in first constructor is :"+ areaInThirdConstructor);
Rectangle rectangle4 = new Rectangle(3.0f, 2.0f);
float areaInFourthConstructor = rectangle4.fourth();
System.out.println(" The area of a rectangle in first constructor is :"+ areaInFourthConstructor);
}
}
class Rectangle {
int l, b;
float p, q;
public Rectangle(int x, int y) {
l = x;
b = y;
}
public int first() {
return (l * b);
}
public Rectangle(int x) {
l = x;
b = x;
}
public int second() {
return (l * b);
}
public Rectangle(float x) {
p = x;
q = x;
}
public float third() {
return (p * q);
}
public Rectangle(float x, float y) {
p = x;
q = y;
}
public float fourth() {
return (p * q);
}
}
Output :
|
C:\Program Files\Java\jdk1.6.0_18\bin>javac ConstructorOverloading .java C:\Program Files\Java\jdk1.6.0_18\bin>java ConstructorOverloading The area of a rectangle in first constructor is : 8 The area of a rectangle in first constructor is : 25 The area of a rectangle in first constructor is : 4.0 The area of a rectangle in first constructor is : 6.0 |
Passing var-args to methods :
Variable length arguments are known as var-args. The method which takes variable number of arguments is called varargs method . Given below method "vaTest( )" excepts variable length arguments. In the below example it accepts -1, 3, and 0 arguments. See below example to understand it clearly :
Example :
public class ClassVarargs {
static void vaTest(int v[]) {
System.out.println("Number of args :" + v.length + "
Contents :");
for (int x : v)
System.out.println(x + " ");
System.out.println();
}
public static void main(String args[]) {
int n1[] = { 10 };
int n2[] = { 1, 2, 3 };
int n3[] = {};
vaTest(n1); // one args
vaTest(n2); // two args
vaTest(n3); // three args
}
}
Example :
|
C:\Program Files\Java\jdk1.6.0_18\bin>javac ClassVarargs .java C:\Program Files\Java\jdk1.6.0_18\bin>java ClassVarargs Number of args :1 Contents : 10 Number of args :3 Contents : 1 2 3 Number of args :0 Contents : |

[ 0 ] Comments