Methods in JavaLast Updated : 31 Dec 2025 In Java, a method is used to do a specific work and helps make the program simple and reusable. What Is a Method in Java?A method is a block of code or a collection of statements or a set of code grouped to perform a certain task or operation. It is used to achieve the reusability of code. We write a method once and use it many times. We do not need to write code again and again. It also provides easy modification and readability of code, just by adding or removing a chunk of code. The method is executed only when we call or invoke it. The most important method in Java is the main() method. If you want to read more about the main() method, refer to the link Java Main Method. Advantages of Using MethodsThe following are the advantages of using methods in Java:
Method Naming RulesWhile defining a method, remember that the method name must be a verb and start with a lowercase letter. If the method name has more than two words, the first name must be a verb followed by an adjective or noun. In the multi-word method name, the first letter of each word must be in uppercase except the first word. For example: Single-Word Method Name: sum(), area() Multi-Word Method Name: areaOfCircle(), stringComparision() It is also possible that a method has the same name as another method in the same class; it is known as method overloading. To read more Java Naming Convention Method DeclarationThe method declaration provides information about method attributes, such as visibility, return type, name, and arguments. It has six components that are known as method header, as we have shown in the following figure. ![]() Method SignatureEvery method has a method signature. It is a part of the method declaration. It includes the method name and parameter list. Access SpecifierAccess specifier or modifier is the access type of the method. It specifies the visibility of the method. Java provides four types of access specifiers:
Return TypeReturn type is a data type that the method returns. It may have a primitive data type, object, collection, void, etc. If the method does not return anything, we use the void keyword. Method NameIt is a unique name that is used to define the name of a method. It must correspond to the functionality of the method. Suppose, if we are creating a method for subtraction of two numbers, the method name must be subtraction(). A method is invoked by its name. Parameter ListIt is the list of parameters separated by a comma and enclosed in a pair of parentheses. It contains the data type and variable name. If the method has no parameter, leave the parentheses blank. Method BodyIt is a part of the method declaration. It contains all the actions to be performed. It is enclosed within a pair of curly braces. The method body is enclosed in curly braces {} and contains the statements that define the functionality or working of the method. For example, consider the following code snippet. Types of MethodsThere are two types of methods in Java:
To read more Types of Methods in Java 1. Predefined MethodThe methods that are already defined in the Java class libraries are known as predefined methods. It is also known as the standard library method or built-in method. We can directly use these methods just by calling them in the program at any point. For example, String.length(), String.equals(), String.compareTo(), Math.sqrt(), Math.pow(), etc, are predefined methods. When we call any of the predefined methods in our program, a series of code related to the corresponding method runs in the background. ExampleCompile and RunOutput: 2 raised to the power of 5 is: 32.0 2. User-Defined MethodThe method written by the user or programmer is known as a user-defined method. These methods can be modified or customized according to the requirements. Calling User defined MethodIn Java, calling or invoking a method depends on whether the method is static or non-static. If the method is static, we can call it directly using the class name or from within the same class. Note that we need to create an object for calling static methods. SyntaxIt has the following syntax: If the method is non-static, we must create an object of the class and use it to call the method. Note: We will get a compile-time error if we try to call a non-static method from a static context without an object because non-static methods belong to instances, not the class itself. ExampleLet us take an example to demonstrate how we can call the user defined function in Java. Compile and RunOutput: Goodbye from a non-static method! Hello from the static method! Next TopicConstructors in Java |
We request you to subscribe our newsletter for upcoming updates.