Method in Java - Definition and Declaration

 Method in Java is a collection of multiple statements that performs some specific operation. 

        Java Methods allows us to use same block of code again and again without rewriting the entire block of statements. Usually, every method is part of a class in Java.
 Method in Java increases the performance by optimizing the code and increasing the code reusability.

Syntax: Method Declaration
              <access-modifier> <return-type> methodName(list of parameters){
                        //body of method
               

For Example:
                public void sayHello(String name){
                    System.out.println("Hello " + name);
                }

                In the above example:
                    public: access-modifier
                    void: return type
                    sayHello: methodName
                    name: parameter

There are the following access modifiers defines in Java
    public: accessible by all classes
    private: accessible only inside the class where it is defines
    protected: accessible inside the same package or sub-classes in different packages
    default: defined without using the default keyword, accessible only inside the same class and package where it is defined.

      To know access modifiers in detail: click here

Based on the requirements there can be 4 types of method definition:
    1. method which accepts no parameter value and returns nothing
           
                    public void sayHello(){
                        System.out.println("Hello World");
                    }
                    //Here the method returns a void means nothing and accepts no parameter value

    2. method which accepts no parameter value and returns something

                    public String sendGreetings(){
                            String message = "Welcome to think design and create";
                            return message;
                    }
                    //Here the method is returning a message of type String without accepting any parameter

    3. method which accepts some parameters value and returns nothing

                    public void sayHello(String name){
                            System.out.println("Hello " + name);
                    }
                    //Here the method is returning nothing while accepting a parameter of type String.
        Note: The method can accept any number of parameter value.

    4. method which accepts some parameters value and returns something

                public int add(int a, int b){
                    int result = a+b;
                    return result;
                }
                //Here the method is accepting two parameters a and b as well as returning an integer value

Calling Java Method:
                To use the method inside a Java Program we need to call the methods using the method name.

                Example 1:
                             public void sayHello(){
                            System.out.println("Hello World");
                          }

                          We can call this function as:
                              sayHello()  //Output: Hello World

                Example 2:
                               public int add(int a, int b){
                                int result = a+b;
                                return result;
                            }    
                            
                            to call this method:
                            int res = add(2, 3)//Since it is returning a value, we can store it in a variable
                            System.out.println(res);  //Output: 5
    






Comments

Popular Posts