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 teh console.
Now you will learn how to create your own methods with or without returning values, invokes a method with or without parameters, and apply method abstration ba batil in the program design.
Creating method:
Consider the following example to explain the syntax of a method:
public static int methodName(int a,int b) { //body, here goes the logic }
here ,
public static: modifier
int : is 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 below:
modifier returnType nameOfTheMethod(parameter list) { //method body }
modifier: it defines the access type of the method and it is optional to use.
returnType: method may return a value
nameOfTheMethod: This is the method name.The method signature consists of the method name and the parameter list.
Parameter List:The list of parameter 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 statements.
Example:
public static int minFunction(int n1,int n2) { int min; if(n1>n2) min=n2; else min=n1; return min; }
Method Calling:
– method returns a value
– returning nothing
example:
public class ExampleMinNumber { public static void main(String[] args){ int a=11; int b=6; int c=minFunction(a,b); System.out.println("Minimum Value = "+c); } public static int minFunction(int n1,int n2){ int min; if(n1>n2) min=n2; else min=n1; return min; } }
method is the just like function in c/c++
output:
Minimum Value = 6
The void keyword:
The void keyword allows us to create methods which do not return a value.here in the void method methodRankPoints().This method is a void method which does not return any value.
example:
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"); } } }
output:
Rank:A1
Passing Parameters by value:
public class SwappingExample { public static void main(String[] args) { int a=30; int b=45; System.out.println("Before swapping, a= "+a+" and b= "+b); //Invoke the swap method swapFunction(a,b); System.out.println("Now before and after swapping values will be"); System.out.println("After swapping, a= "+a+" and b is "+b); } public static void swapFunction(int a,int b) { System.out.println("Before swapping(Inside), a= "+a+" b= "+b); // int c; c=a; a=b; b=c; System.out.println("After swapping(Inside),a="+a+" b= "+b); } }
output:
Before swapping, a= 30 and b= 45 Before swapping(Inside), a= 30 b= 45 After swapping(Inside),a=45 b= 30 Now before and after swapping values will be After swapping, a= 30 and b is 45
Method Overloading:
When a class has two or more methods by same name but different parameters, it is known as method overloading.It is different from overriding.In overriding a method has same method name,type,number of parameters etc.
example:
public class ExampleOverLoading { public static void main(String[] args) { int a=11; int b=6; double c=7.3; double d=9.4; int result1=minFunction(a,b); //same function name with different paramaters double result2=minFunction(c,d); System.out.println("Minimum value= "+result1); System.out.println("Minimum value= "+result2); } //for integer public static int minFunction(int n1,int n2) { int min; { if(n1>n2) min=n2; else min=n1; } return min; } //for double public static double minFunction(double n1,double n2) { double min; if(n1>n2) min=n2; else min=n1; return min; } }
output:
Minimum value= 6 Minimum value= 7.3
Using Command Line Arguments:
Sometimes you need to pass command to a program by command line arguments into main()
The Constructors:
Parametarized constructor:
Here is a simple example that uses a constructor with parameter:
public class MyClass { int x; //Following is the constructor MyClass(int i) { x=i; } }
You would call constructor to initialize objects as follows:
public class ConsDemo { public static void main(String[] args) { MyClass t1=new MyClass(10); MyClass t2=new MyClass(20); System.out.println(t1.x+" "+t2.x); } }
output:
10 20
The this keyword:
this is a keyword in Java which is used as a reference to the object of the current class, with an instance method or a constructor.Using this you can refer the members of a class such as constructors,variables and methods.
Note:The keyword this is used only within instance methods or constructors.
In general the keyword this is used to:
Differentiate the instance variables from local variables if they have same name, within a constructor or a method.
class Student{ int age; Student(int age) { this.age=age; } }
– Call one type of constructor(Parametrized constructor or default ) from other in a class.It is known as explicit constructor invocation.
Class Student{ int age; Student() { this(20); } Student(int age) { this.age=age; } }
This এর ব্যাপারটা আরো ক্লিয়ার করতে হবে পরে
Variable Arguments(var-args)
typeName…parameterName
public class VarArgsDemo { public static void main(String[] args) { //Call method with variable args printMax(34,3,3,2,56.5); printMax(new double[]{1,2,3}) } public static void printMax(double...numbers) { if(numbers.length==0) System.out.println("No argument passed"); return; } double result=numbers[0]; for(int ) }
there is another method finalize() method
syntax:
protected void finalize() { //finalization code here }