Java Methods/Java function

Functions are used to combine multiple statement and instruction.This statement is depending upon which type of operation will be performed by this method. So directly or indirectly function are solve any particular task. The main advantage of function are that is used to provide reusability of code. That is define are only once. And possible to used so multiple times in our program. that is depends based on situations and our requirement. There can be defined using of this syntax.

modifiers retrunType methodName(args){
    //body of function  
    //define statement and logic here 

Parameter is defined of within parentheses () and more than one parameter is separated by is comma. body of function statement is define between braces {}. When define of a function in java programming there are remember to following important points.

Body of function: Everything of function, behaviours is depends upon what kind of operation it will perform. If that is basic function and used by other class then set that its modifiers type will public (use same class and inherit class). But if this is very special function and very secure to outside of class then we set its modifier type are private.

Function parameters: When function are perform some operation and there is need some special information. for example display employee salary information. In this situation we can used employee id to get its information. So id is required by this function. so function are get this information in parameter section and perform operation. this parameter is called argument of function. There is possible are function are get more than one parameter argument. In this situation compiler is not smart.

In this case programmer responsible to provide information of which type of value are accepted by function parameter and how many number of parameters it will need.

Return Type: Finally executes all statement by function. and that function are not return any value. so define function return types is void. otherwise provide its return type in after of modifier.

Modifier: is an access controller of the function within the class and inherit class. that is implement encapsulation of data members. By default access specifier of method will be public.

This is basic overview of function but function execution is start when it is call. let's see an example to define and use a method.

class Example{
  //Define a method which are accept one parameter
    void say(String info){
      //Display result
      System.out.println("Hello "+info);

  public static void main(String[] args) {
    Example obj=new Example();

    obj.say("Programmer");//Execute method
    obj.say("Developer");//Execute method
    obj.say("Tester");//Execute method
Function calling
Hello Programmer
Hello Developer
Hello Tester

Example of User Defined function

There is four way to define and use of user functions in programming. firstly view few basic example.

  class Example{
    *  Function : add
    *  parm: two integers value
    *  @return type: void
    *  modifier : private
    private void add(int num1,int num2){
      System.out.println("Sum is : "+(num1+num2));
    *  Function : divide
    *  parm: two float value
    *  @return type: float
    *  modifier : private
    private float divide(float num1,float num2){
      if(num2==0) return 0; 
      //when divisor not zero
      return num1/num2; //return result of this statement

  public static void main(String[] args) {
    Example obj=new Example();
    obj.add(1,4);//call add function

    //passing two float variable and 
    //return value is stored by result variables
    float result=obj.divide(8.0f,2.0f); //call divide function

    //display result value
    System.out.println("Divide result :"+result);



Sum is : 5
Divide result :4.0

In this example there is two user defined function. add() and divide(). first executed function in this program are main(). inside that function executed add function.

Execute function

So function are have 4 types. That is based on returning values, argument list. for example.

Accepting and returning values : This type of function are useful when function are get values as parameters and resultant of function is returned.

Accepting and not returning values : Few function are accepted formal parameters but that are not returning any value. generally this type of function are used to set the value of class data member value.

Not accepted and returning of values In type of function not accept any value but that is returning data member value or other useful value function.

Not accepted and returning any values: In type of function not accept any value but that is returning data member value or other useful value function.

Arbitrary Argument of method

This type of method are capable to retrieve any number of parameter list. This parameter type are defined of this syntax.

methodName(dataType ... variableName)

Parameter value of this method are internally create an array. And we can use this parameter values, like that used an array element by index values. For example.

class ArbitraryArgument
    public void record(int ... record){

      for(int data : record){
        System.out.print("  "+data);
    public static void main(String[] args){

      ArbitraryArgument obj = new ArbitraryArgument();
      obj.record(1, 4, 3);
Arbitrary Argument Example
  1  4  3
  7  2
  5  2  8  22  4

Overloading Methods

Overloading is a way to define similar method name but there parameter argument types and sequence are different. return type not matter in method overloading. Let's see an example of method overloading.

class Argument
    public void test(char a){
      System.out.println("Char a : "+a);
    public void test(int a){
    System.out.println("Int a : "+a);
    public void test(int a,int b){
    System.out.println("Int b : "+b);
    public void test(int a,int b,int c){
    System.out.println("Int c : "+c);
    public void test(int b,int c,double a){
      System.out.println("Double a : "+a);
    public static void main(String[] args){

      Argument obj = new Argument();
Char a : A
Int a : 10
Int b : 1
Int c : 2
Double a : 10.2
Method Overloading Example

In this example test method are called another different variant of test method. This function calling execution are based on compile time.

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment

© 2021,, All rights reserved