Oop Java

Method in Java programming language with example

Method in Java programming language  with example

In this tutorial, We will learn about Method in Java programming language with an example.

Java method example

Method in Java

The method is an important concept in Object-oriented programming languages(Oop). In the Java programming language, a method is a segment of code referred to by a name that does a particular task. A Java method has a collection of statements that are grouped together to perform a particular operation.

 

A particular method can contain two section

method header – method header should be two factor

  • return type  variable – void, int, float, double etc
  • method name – user can define(User define method)

method body – method body is enclosed using braces (curly brackets)and it has a group of statements to perform a particular task

 

Two type methods available in Java

  1. Pre-defined method – Standard Library Methods
  2. User-defined method

Pre-defined method – Standard Library Methods

These methods are in-build methods, already defined in the Java Library file. They are ready to be used at any time.

For example

  • Math function in Java  – sqrt(), log()
  • String function in Java – str.length()

 

User defined method in Java

Inside the class, you can define a method to perform a particular task according to your purpose. It is known as a user-defined method.

Declaration

Syntax of method

modifier return_type method_name(list of parameter){
method body
}

Here

modifier – public private protected and static

return type – it is a section of the method used to denote the returned value.

All data types except void are functioning return type.

method name(user define)– Some of the rules should be applied to the method name.

Parenthesis() – we can pass a list of parameter separated with commas inside the Parenthesis().

 

Example of method

public static int get_Sum(int a, int b){ //parameter is an optional part in method
statement(s)
//method body
}

Here,

public, static – access modifier

int – a return type which may return a value to the main method other than when the void is used.

get_Sum – method name – it is used to identify a particular method

int a, int b – list of parameter

If we use no parameter or empty argument, it is known as the default parameter.

The method body( braces(curly brackets)) includes the codes including the statements.

 

Java has three type of method

  1. The dumb method– Does the same thing every time.
void add(){
int x=5;
int y=7;
System.out.println(a+b);
}

Program 1

//Example for dumb method
public class dumbmetod1{
public static void main(String args[]){ //main methid
add();//call the method inside the main method
        }
public static void add(){
int a=25,b=35; //user define method
System.out.println("Total is :"+(a+b));

    }
  }

 

When the above code is executed, it produces the following results:

Total is : 60

 

 

2. Clever method

Ask for an input value

Eg - void add(int a, int b){
System.out.println(a+b);
}

Program 2

//Example for clever method
public class Clever_method1{
public static void main(String args[]){
add(40,25);//call the method inside the main method
        }//with argument
public static void add(int a, int b){//parameter
System.out.println("Total is :"+(a+b));
System.out.println("Different is :"+(a-b));
System.out.println("Multiplication is :"+(a*b));
System.out.println("Division is :"+(a/b));
    }
  }

 

When the above code is executed, it produces the following results:

Total is :65
Different is :15
Multiplication is : 1000
Division is :1

 

2. Smart method

Ask for input value and returns the result to the main method

Eg - void add(int a, int b)
{
int c=a+b;
return c;
}

 

Program 3

//Example for smart method
public class Smart_method1{
public static void main(String args[]){
  System.out.println("Total is :"+add(65,35));
}
public static int add(int a, int b){
int sum=a+b;  //user defined method with parameter
return sum;//return value to main method

    }
  }

 

When the above code is executed, it produces the following results:

Total is :100

 

 

How to call a method

If you defined a method for your significant task, you can use this method on your program

You have to call the method, like this

method_Mame();
my_method();

It is possible only if you have already defined a method

Example for calling the method in the program

public class Class_name{
public static void main(String args[]){ //main method
my_Metod();//call the method inside the main method
}
public static void my_Method(){
statement(s) //method body

}
}
calling method

When we call the method, How method works?

1 – In java language, firstly, you should call your method inside the main method.

2 – When Java program codes are being executed,  it meets the methods inside the main methods and executes the declaring of the method through the method name.

3 – Then the executed results from the method returns to the main method.

 

Program 1

public class Method_example{
  
  public static void my_Method(){//user define method
        System.out.println("This is the property of my method:");
    }//end of user define method
    
    public static void main(String args[]){ //main method
    System.out.println("call the method from inside the main method:");
      my_Method();//call the method inside the main method
        }//end of method
  }

 

When the above code is executed, it produces the following results:

call the method from inside the main method
This is the property of my method:

 

Program 2

When we declare two separate classes how to call a method from one to another

class Class_main1{//class_one

public static void main(String args[]){
    
Class_U_method obj=new Class_U_method();
//create object for class_U_method

obj.my_method();//calling the metod
//method was executed successfully here
}
}

class Class_U_method{//class_two
public void my_method(){ //user defined method as public
System.out.println("Display statement from inside user defined method");
}//method statements here
}

When the above code is executed, it produces the following results:

Display statement from inside user defined method

Java Method and return value

A Java method can have either empty or more parameter and they return a value.

Let’s see a very simple example to understand how to Java method returns value during the addition of two numbers

Program 1

class calc1{
public static void main(String args[]){
    
 System.out.println(add_Num());//Call the method
}
static int add_Num(){//Java method with Zero parameter
return (5+10);//return statement
//return value to main method
}

}

return is a Java keyword used to return value from a method.

When the above code is executed, it produces the following results:

15

 

Just simple modification at the above  program for easy understanding

class calc2{
public static void main(String args[]){
    int result=add_Num();//assign the output to another variable
 System.out.println(result);//display result
}
static int add_Num(){
return (5+10);

}

}

When the above code is executed, it produces the following results:

15

 

Same output

Now we will try to clearly understand return statement – How it works

return value to the main method

According to the example, this method always returns the same value (added value of 15) to the main method.

Java Method and return value with the argument

Same thing(return value) but with the addition of simple modification of the above program with the argument

Program 1

class calc3{
public static void main(String args[]){
    int result=add_Num(5,10);//argument
 System.out.println(result);
}//main metod

static int add_Num(int a,int b){//parameter
return (a+b);
//user defined method
}

}

When the above code is executed, it produces the following results:

15

Let’s try to understand how the above program works

how methods words

Multiple methods and multiple argument inside a class

Program 1

class calc4{
public static void main(String args[]){
    
 System.out.println("sum of 20 + 10 ="+add_Num(20,10));//argument 1
 System.out.println("Different of 20 - 10 ="+sub_Num(20,10)); //argument 2
 System.out.println("multiply of 20 * 10 ="+mul_Num(20,10)); //argument 3
 System.out.println("division of 20 / 10 ="+div_Num(20,10)); //argument 4
}

static int add_Num(int a,int b){//parameter 1
return (a+b);
}

static int sub_Num(int a,int b){//parameter 2
return (a-b);

}

static int mul_Num(int a,int b){//parameter 3
return (a*b);

}

static int div_Num(int a,int b){//parameter 4
return (a/b);

}
}

When the above code is executed, it produces the following results:

 

sum of 20 + 10 = 20
Different of 20 - 10 = 10
multiply of 20 * 10 = 200
division of 20 / 10 = 2

 

Calculate the total value of 1 t0 100:

Program

class calc5{
public static void main(String args[]){
    add_Num();//method call 
}

static int add_Num(){
    int sum=0;
 //calculate 1 to 100 positive integer numbers
 for(int i=1; i<=100; i++){
 sum=sum+i;
 
}
System.out.println("Total value is :"+sum);
return (sum);
}

}

When the above code is executed, it produces the following results:

Total value is :5050

 

Advantages of the method in Java

  1. A method is created using a set of statements for a specific task. So it can be used as you choose.
  2. Code re-usability –  method can be created only once and used multiple time.
  3. As the method is a group of statements, the bulk of methods can be used to create programmes. It is readable, understandable and debugging is very easy.

Suggested for you

User define function in Python

User define function in C language

Math function in C language

String function in C language

Interface in Java programming language
Constructor overloading in Java programming
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago