Table of Contents
method in Java programming language
In this tutorial. we will discuss the method in the Java programming language
Java method
{
Method body
}
Access modifier – public private,protectedand others
return type= The data type of te value returned by the method
Eg
- void – method does not return a value
- int – Method return a value
3. The method name – Some of rules should be apply the method name
4. The parameter list in parenthesis – you can pass list of input parameter separated with comma and enclosed the parenthesis (parameter list), if you use no parameter you must use empty breckets ()
5. The method body, Enclosed, between braces(curly brackets) the method,s code including the declaration of local variable goes here.
Java method types
Java language has two types of methods
- standard library method
- User-defined method
Standard library method
Standard library methods are pre-defined (build-in) method in Java language. build in methods are part of the java compilar.
User-defined method
User can define the method as their wish in Java language. it called user-defined method
Java has three types of user defined methods
Three types of method available in Java
1. The Dumb Method
Does the same thing every time
Eg – void add()
{
int a=5,b=7;
System.out.println(a+b);
}
Example
class dumbmethod{
public static void main(String args[]){
add();
}
public static void add()
{
int a=15;
int b=25;
System.out.println(a+b);
}
}
2. The Clever Method
Ask the value for the input
Eg – void add(int a, int b)
{
System.out.println(a+b);
}
class clevermethod{
public static void main(String args[]){
add(24,34); //argument passing
add(40,67);
}
public static void add(int a,int b) //parametter passing
{
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
}
}
3. The Smart Method
Ask the value for input and also returns a result to the main method
Eg – void add(int a, int b)
{
int c=(a+b);
return c;
}
program 1
class smartmethod{
public static void main(String args[]){
int x=add(24,34);
System.out.print(x);
}
public static int add(int a, int b)
{
int sum=a+b;
return(sum);
}
}
How to create a user-defined method
we can declare a user-defined method as follows
Method definiyions have four basic parts
Metod name – Name of te method
return type – the variable type identifies the method return a value or not
parameter list – A list of parameter, we can pass one or more parameter
Method body – Statements inside the method body
No argument metod
public static void my_method() { //No parameter method System.out.println("I am a user-defined method in Java"); }
Here, we have defined a Java method named as my_Method() with no argument.
We can use three keywords public, static, void in the initial part of the method name
Method with argument
public static int my_method(int a, int b) { //The method with parameter System.out.println("I am a user-defined method in Java"); }
Public static – Access modifiers
int – return type
my_Metod – The name of the method
int a, int b– list of parameter
How to call Java method
Example program using metod
{
{
}
{
{
Java method with argument and return value
program 1
public class javamethod5{ public static void main(String args[]){ int sum=add(100,200,300,400); //argument passing System.out.println(sum); int result=sum*15; System.out.println(result); //display result } public static int add(int a,int b,int c,int d) //parametter passing { //System.out.println(a+b+c+d); return(a+b+c+d); //return value to main method } }
When the above code is executed, it prodeces the following result
program 2
class findmax{ public static void main (String args[]){ int result; result=max(23,45); System.out.println(result); } public static int max(int a, int b) {int max; if(a>b) { max=a; } else { max=b; } return max; } }
When the above code is executed, it prodeces te following result
Related post