Rohit Gupta
What do you mean by Methods in Java?
By Rohit Gupta in Java on Sep 10 2019
  • Laxmidhar Sahoo
    Jan, 2020 22

    1. A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. Think of a method as a subprogram that acts on data and often returns a value.
    2. Methods allow us to write reusable code and dividing our program into several small units of work
    3. When a method is invoked (called), a request is made to perform some action, such as setting a value, printing statements, returning an answer, etc. The code to invoke the method contains the name of the method to be executed and any needed data that the receiving method requires. The required data for a method are specified in the method's parameter list.
    4. Java Method Components
    5. Access Modifiers: Java Method access modifiers defines who can call the method. It’s used to restrict the scope of the method. There are four access modifiers in java – private, protected, public and default. Apart from access modifiers, we can also specify a method to be static, in that case, we don’t need to create an object to call the method. We can directly call a static method through class. Static methods are useful for creating utility methods.
    6. Return Type: Java Methods must specify the return type. They can be any primitive type or an object. If the method is not returning anything, then we must use void as return type.
    7. Method Name: Every method in Java must have a name. It’s used to identify the method. We should provide descriptive names for our method to give some idea about the task performed by the method. If you have written any Java program, I am sure you would have seen java main method.
    8. Method Parameters: We can pass parameters to a method, they are defined in the parenthesis after the method name. Usually, method statements work on these parameters to achieve their tasks.
    9. Exceptions List: Sometimes a java method can throw exceptions. We can define them using throws keyword. If there are multiple exceptions that can be thrown, then we can separate them using comma.
    10. Method Body: This is where all the method operations take place. Method body contains statements to be executed by the method and they are inside the curly brackets.

    • 0
  • Vijay Kumari
    Sep, 2019 16

    A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions.

    We use methods in Java for To reuse code define the code once, and use it many times.
    For a detailed tutorial on methods in Java, https://www.c-sharpcorner.com/article/a-complete-java-classes-tutorial/

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS