Some pre-defined methods are length(), equals(), compareTo(), sqrt(), etc. We can directly use these methods just by calling them in the program at any point. It is also known as the standard library method or built-in method. In Java, predefined methods are the method that is already defined in the Java class libraries is known as predefined methods. It is also possible that a method has the same name as another method name in the same class, it is known as method overloading. Multi-word method name: areaOfCircle(), stringComparision() In the multi-word method name, the first letter of each word must be in uppercase except the first word. If the method name has more than two words, the first name must be a verb followed by adjective or noun. While defining a method, remember that the method name must be a verb and start with a lowercase letter. It is enclosed within the pair of curly braces. It contains all the actions to be performed. Method Body: It is a part of the method declaration. If the method has no parameter, left the parentheses blank. It contains the data type and variable name. Parameter List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. Suppose, if we are creating a method for subtraction of two numbers, the method name must be subtraction(). It must be corresponding to the functionality of the method. Method Name: It is a unique name that is used to define the name of a method. If the method does not return anything, we use void keyword. It may have a primitive data type, object, collection, void, etc. Return Type: Return type is a data type that the method returns. It is visible only from the same package only. Default: When we do not use any access specifier in the method declaration, Java uses default access specifier by default.Protected: When we use protected access specifier, the method is accessible within the same package or subclasses in a different package.Private: When we use a private access specifier, the method is accessible only in the classes in which it is defined.Public: The method is accessible by all classes when we use public specifier in our application.Java provides four types of access specifier: It specifies the visibility of the method. It includes the method name and parameter list.Īccess Specifier: Access specifier or modifier is the access type of the method. Method Signature: Every method has a method signature. It has six components that are known as method header, as we have shown in the following figure. The method declaration provides information about method attributes, such as visibility, return-type, name, and arguments. If you want to read more about the main() method, go through the link. The most important method in Java is the main() method. The method is executed only when we call or invoke it. It also provides the easy modification and readability of code, just by adding or removing a chunk of code. We do not require to write code again and again. We write a method once and use it many times. It is used to achieve the reusability of code. What is a method in Java?Ī method is a block of code or collection of statements or a set of code grouped together to perform a certain task or operation. In this section, we will learn what is a method in Java, types of methods, method declaration, and how to call a method in Java. We can also easily modify code using methods. Similarly, the method in Java is a collection of instructions that performs a specific task. In general, a method is a way to perform some task.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |