Java Method – Declaration and Calling a Java Method

1. Objective

In our last tutorial, we discuss Java Inner Class, now we are going to learn what is the Java method? Moreover, we will discuss:

  • Java method types
  • How to declare a method in java?
  • How to call a Java method?
Java Method - Declaration and Calling a Java Method

Java Method – Declaration and Calling a Java Method

So, let’s start the Java  Method Tutorial.

2. What is the Java Method?

Java Methods is basically a collection of statements in Java or Java commands which are used to run a particular task and return the result or value to the caller, it can also perform a task without returning any value. They also provide the user to use a particular code again without retyping. A Java method must always be a part of a class in Java, which is unlike C, C++, and Python.

Follow this link to know about Command Line Arguments In Java

3. Method Declaration in Java

Java Method declaration has 6 components in general, they are

a. Java Modifiers

A modifier in Java defines the access type of the method, i.e. it defines in which part of the program it can be accessed.
Java has 4 types access modifiers

  • Public – In this type, the method is accessible in all classes in the application.
  • Protected – Here, the method is accessible in the class in which it is defined and also in its subclass(es).
  • Private – In this type, the method is accessible only in the class it is defined in.
  • Default – In this type, the method is accessible only within the same class and package in which it is defined.

b. Return type in Java

The return type defines the type of data which is returned by the method and also it is void when nothing is returned.

c. Java Method Name

Method name in Java is used to define the name of a method, it is same as the naming of field types with a little difference in the convention.

d. Java Parameter list

The parameter list in Java is a list with input parameters that are separated by commas, they are preceded by their datatypes within closed parenthesis, and also, if there are no parameters then there is an empty parenthesis ().

e. Java Exception list

The exception list in Java is a list of exceptions which can be expected during the run, all these exceptions must be specified.

Read about Exception Handling in Java

f. Method body in Java

The body of the Java method must be enclosed within braces, all the codes for the method are written in it.

Java method body

Java Method Body

g. Java Method signature

A method signature in Java is consists of method name and parameter list in Java i.e. number, type, and order of parameters. Java return type and exceptions are not a part of method signature.

4. How to Call a Method in Java?

A Java method is called according to its functionality, it is called in any of these 3 situations

  • When it completes all the statements
  • An exception is thrown to it
  • When it reaches a return statement

See Also – Java Number Method
Example 

import java.io.*;
class Addition
     {
         int sum = 0;
         public int addTwoInt(int a, int b)
               {
                  // adding two integer value.
                     sum = a + b;
                 //returning summation of two values.
                     return sum;
               }
class GFG
     {
       public static void main (String[] args)
             {
                // creating an instance of Addition class
                   Addition add = new Addition();
               // calling addTwoInt() method to add two integer using instance created in above step.
                  int s = add.addTwoInt(1,2);
                 System.out.println("Sum of two integer values :"+ s);
             }
     }

Example to illustrate the Java method in detail 

// Java program to illustrate different ways of calling a method
import java.io.*;
class Test
      {
        public static int i = 0;
// constructor of the class which counts the number of the objects of the class.
        Test()
           {      
              i++
           }
// static method is used to access static members of the class and for getting total no of objects of the same class created so far
public static int get()
              {
// statements to be executed....
                return i;
              }
// Instance method calling object directly that is created inside another class 'GFG'. Can also be called by an object directly created in the same class and from another method defined in the same class and return integer value as the return type is int.
public int m1()
        {
          System.out.println("Inside the method m1 by object of GFG class");
// calling m2() method within the same class.
this.m2();
// statements to be executed if any
return 1;
         }
// It doesn't return anything as return type is 'void'.
public void m2()
       {
           System.out.println("In method m2 came from method m1");
        }
    }
class GFG
     {
        public static void main(String[] args)
            {
// Creating an instance of the class
               Test obj = new Test();
// Calling the m1() method by the object created in above step.
               int i = obj.m1();
               System.out.println("Control returned after method m1 :" + i);
// Call m2() method
// obj.m2();
               int no_of_objects = Test.get();
               System.out.print("No of instances created till now : ");
               System.out.println(no_of_objects);
          }
     }

The control flow of the program above 

Do you know How Java Inheritance is used

Java Method - Control Flow

Java Method – Control Flow of Programm

So, this was all about Java Method Tutorial. Hope you like our explanation.

5. Conclusion

Hence, we learned about the basic concepts of java method- declaration and Calling. In addition, we saw components of a declaration of Java method are: modifiers, Return type, Java method name, parameter list, exception list and methods signature. Still, have a confusion? Share with us, surely we will get back to you!

See also – Method Overriding in Java
For reference

2 Responses

  1. natalia says:

    Simply wɑnna input on few gеneral things, The website layout is perfect, articles are fantastic

  2. Mohammed Tasi'u says:

    Hello! I created a window using java, and I want to put color to the background and I don’t know how to do so. Help!

Leave a Reply

Your email address will not be published. Required fields are marked *