Java Methods

In All programming languages, Java methods are a way to execute some task. Likewise, the method in Java is a collection of instructions that performs a particular task. We write java methods once and use them many times. We do not need to write code again and again. Java method also provides the easy modification and readability of code, simply by adding or eliminating a chunk of code. The Java methods are executed only when we call them.

How to Create Java Methods?

Syntax of Java Methods:

AccessSpecifier returnType methodName( parameter list) {
  // method body
}

Here, in above mentioned syntax are as follows:

Access Specifier: 

Access specifier is the access type of the method. Which specifies the visibility of the method. Java allows four types of access specifier:

  • Public: This Java method is accessible by all classes when it is used as a public specifier in an application.
  • Private: A private access specifier is accessible only in the classes in which it is declared.
  • Protected: A protected access specifier is accessible in the same package or subclasses in a different package.
  • Default:  Java uses the default access specifier default when we do not use any access specifier in the method declaration, It is visible only in the same package only.

Return Type:

Return type is nothing but a data type which returns the value with their type. It can be a primitive data type, object, collection or void, etc. If a method does not return any value, then we use void keyword.

Method Name:

Java method name is unique name which we use to define the name of a method. It must be according to the functionality of the method. Just for example, if you are creating a method for addition of two numbers, the method name should be addition(). A method is call by its name in a program.

Parameter List: 

It is the list of parameters separated by a comma and enclosed in the pair of parentheses (). The parameter list contains the data type and variable name. If a method does not contain any parameter then left the parentheses blank.

There are some rule regarding how to name a method in java:

  • The method name must be a verb.
  • the method name must start with a lowercase letter.
  • The method name can be single or 2-3 words on your requirements.
  • The first letter of each word must be in uppercase except the first word.

For example:

One word method name: add(), square()

Multi-word method name: areaOfCircle(), stringComparision()

What are the types of Java methods?

Method in java are two types.

  1. Predefined Method or  built-in method
  2. User-defined Method

1. Predefined Method:

Java predefined methods are already defined in the Java class libraries. This is also known as the standard library method or built-in method. We can directly call them in the program at any point. When we call any of the built-in methods in a program, a piece of code related to the called method runs in the background that is already stored in the java library. Some Java built-in methods are length(), equals(), compareTo(), sqrt(), etc. 

public class Example   
{  
public static void main(String[] args)   
{  
// using max() method of Math class  
System.out.print("The maximum number is:" + Math.max(10,7));  
}  
}  
OutPut:
The maximum number is: 10

2. User-defined Method:

A user-defined method is nothing but a java method that is defined by a user. These types of methods are modified according to the requirement of a user.

How to create and call a User-defined Method?

// creating a method
  public static int squareMethod(int num) {

    // returning statement
    return num * num;
  }

  public static void main(String[] args) {
    int r;

    // calling the method
    // storing returned value to result
    r = squareMethod(10);


    System.out.println("Squared value of 10 is: " + r);
  }
}

Output:

java methods

FAQs:

1. What is difference between static and public keyword in Java?

Ans: The static keyword is used when a resource (variable, method) is in shareable mode i.e. if that resource is used by many objects, then by saving that resource constant, we can save a lot of memory. When we use the public keyword with a resource, it means that we can access that resource everywhere. The public is an access modifier.
Source: Quora

2. What is the work void keyword in java?

The Void keyword is used in many programming languages. In JAVA, It is used when we do not want our method or function to return anything.

3. What happens if you remove static modifier from the main method? 

Ans: The Java Program will compile successfully. But at runtime an error “NoSuchMethodError” will occur . 

4. Why the main method is static in Java?

Ans: A Static method of a class can be called by using the class name only without creating an object of a class. that why the main method is always static because the main method is called by JVM directly without making any intense of class.

Must Read:

This is all about Java methods, If you have any questions regarding this then please raise your query in the comment section. If you want more FAQs on Java then please visit my friend’s site Learnwithsikha. She has written many questions not only in java also in other programming languages.

Thank You 🙂

Leave a Reply

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