Methods in java

Methods in Java or Java methods is a powerful and popular aspect of Java programming.

What are Methods in Java?

A method in Java is a block of code that, when called, performs specific actions mentioned in it. For instance, if you have written instructions to draw a circle in the method, it will do that task. You can insert values or parameters into methods, and they will only be executed when called. They are also referred to as functions. The primary uses of methods in Java are:

 

  • It increases code readability
  • It allows code reusability (define once and use multiple times)
  • You can break a complex program into smaller chunks of code

How to Declare Methods in Java?

You can only create a method within a class. There are a total of six components included in a method declaration. The components provide various information about the method.

Below is the syntax to declare a method and its components list.

public int subNumbers (int a, int b){

//method body

}

Access specifier:

It is used to define the access type of the method. The above syntax sees the use of the “public” access specifier. However, Java provides four different specifiers, which are:

  • Public: You can access it from any class
  • Private: You can access it within the class where it is defined
  • Protected: Accessible only in the same package or other subclasses in another package
  • Default: It is the default access specifier used by the Java compiler if we don’t mention any other specifiers. It is accessible only from the package where it is declared

ReturnType:

It defines the return type of the method. In the above syntax, “int” is the return type. We can mention void as the return type if the method returns no value.

Method name:

It is used to give a unique name to the method. In the above syntax, “addNumbers” is the method name. This tutorial looks at some tips for naming a method, shortly.

Parameter list:

It is a list of arguments (data type and variable name) that will be used in the method. In the above syntax, “int a, int b” mentioned within the parentheses is the parameter list. You can also keep it blank if you don’t want to use any parameters in the method.

Method signature:

You don’t have to do anything additional here. The method signature is just a combination of the method name and parameter list.

Method body:

This is the set of instructions enclosed within curly brackets that the method will perform.

public class MehtodsExamples{

Public int addNumber(int x,int y)
{
int addition =x+y;
return addition;

}
public static void main(String arg[]){

int i=10;
int j=25;
MethodExample obj =new methodExamples();4
int result =Obj.addNumbers(i.j);
System.OuPrintln(“Sum of x+Y =”+ result);

}

 

DeclareMethodsResults

In the above example:

  • ‘Public’ is the access specifier
  • The return type is ‘int’ (i.e. integer)
  • The method name is addNumbers
  • int x and int y are the parameters
  • addNumbers (int x, int y) is the method signature
  • The method body is:

{

int addition = x + y;

return addition;

}

Adding an Example would be great to increase the clarity about the concept

How to Name a Method?

Some of the rules and tips to name the methods in Java are:

 

 

  • For a multi-word name, the first word should be a verb followed by a noun or adjective without any space and with the first letter capitalized (Ex: addIntegers(), areaOfSquare,areaoftrriagle())
  • Try to use a name that corresponds to the functionality (if the method is adding two numbers, use add() or sum())
  • The method name should start with a verb and in lowercase (Ex: sum(), divide(), area(),Volume();substract();)

 

Related Posts

Leave a Reply

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