Java Tutorial: 8(Methods In JAVA)

Methods in Java are the group of statements which perform a particular task and can be invoked at any point in the class using its name.
Each method have its own distinct name and parameters.Methods are time savers, in that they allow for the repetition of sections of code without retyping the code.  In addition, methods can be saved and utilized again and again in newly developed programs. There are 2 types of methods in JAVA which are as follows:
1. Built-In : Build-in methods are part of the compiler package, such as System.out.println( ) and                            System.exit(0).
2. User-defined: User-defined methods are created by you, the programmer. These methods take-on                              names that you assign to them and perform tasks that you create.

Creating a Method in JAVA is easy but it includes different modifiers and parameters which make them distinct. Below is the example:

public Static String myMethod (int x)
{
     //Set of statements to perform a Task
    //return a string
}
public static − modifier - It defines the access type of the method and it is optional to use.

Sring − return type - Method may return a value.

myMethod − name of the method

int x − list of parameters- The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters.

Syntax :
modifier returnType nameOfMethod (Parameter List) {
   // method body
}

Calling the Method in JAVA:
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.

Example:
      String str = Console.myMethod("Enter a number");

Start from here: JAVA Tutorial : Beginner 
Learn Salesforce: Salesforce DX : An Overview

Comments