Java Methods
What is Java Method?
- A method is a block of code that performs a specific task
- A method in Java is a set of instructions that can be called for execution using the method name
- A Java method can take in data or parameters and return a value - both parameters and return values are optional.
- Methods can be public, private or protected. Method is a set of statements to perform an operation,methods are also known as procedures or functions
- A Java function is a block of code designed to perform a specific task, encapsulated within a class or interface
Naming a Method
- In Java language method name is typically a single word that should be a verb in lowercase or a multi-word, that begins with a verb in lowercase followed by an adjective, noun. After the first word, the first letter of each word should be capitalized.
Rules to Name a Method:
- While 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, findSum, computeMax, setX, and getX.
Why are methods used in Java?
It allows code reusability (define once and use multiple times) You can break a complex program into smaller chunks of code
1. Code Reusability: Methods allow you to write a block of code once and reuse it multiple times throughout your program. This saves time and effort, and makes your code more efficient.2. Modularity: Methods help break down complex programs into smaller, manageable parts. This improves code readability and makes it easier to understand, debug, and maintain
Naming a Method
- In Java language method name is typically a single word that should be a verb in lowercase or a multi-word, that begins with a verb in lowercase followed by an adjective, noun. After the first word, the first letter of each word should be capitalized.
Rules to Name a Method:
- While 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, findSum, computeMax, setX, and getX.
Why are methods used in Java?
It allows code reusability (define once and use multiple times) You can break a complex program into smaller chunks of code
1. Code Reusability: Methods allow you to write a block of code once and reuse it multiple times throughout your program. This saves time and effort, and makes your code more efficient.
2. Modularity: Methods help break down complex programs into smaller, manageable parts. This improves code readability and makes it easier to understand, debug, and maintain
Syntax of the methods in Java?
Method Signature: Every method has a method signature. It is a part of the method declaration. It includes the method name and parameter list. Return Type: Return 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 void keyword.
Method Name: It is a unique name that is used to define the name of a method. It must be corresponding 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 List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It contains the data type and variable name. If the method has no parameter, left the parentheses blank.
Method Body: It is a part of the method declaration. It contains all the actions to be performed. It is enclosed within the pair of curly braces
Types of Methods in Java
1. Predefined Method predefined methods are the method that is already defined in the Java class libraries is known as predefined methods. It is also known as the standard library method or built-in method2. User-defined MethodThe method written by the user or programmer is known as a user-defined method.
1.Instance Method : Access the instance data using the object name. Declared inside a class 2.Static Method : Access the static data using class name. Declared inside class with static keyword 3.Abstract Method : abstract methods mean "you have to implement it later point of time" type method ..
Method Signature: Every method has a method signature. It is a part of the method declaration. It includes the method name and parameter list.
Return Type: Return 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 void keyword.
Method Name: It is a unique name that is used to define the name of a method. It must be corresponding 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 List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It contains the data type and variable name. If the method has no parameter, left the parentheses blank.
Method Body: It is a part of the method declaration. It contains all the actions to be performed. It is enclosed within the pair of curly braces
Types of Methods in Java
1. Predefined Method
predefined methods are the method that is already defined in the Java class libraries is known as predefined methods. It is also known as the standard library method or built-in method
2. User-defined Method
The method written by the user or programmer is known as a user-defined method.
1.Instance Method : Access the instance data using the object name. Declared inside a class
2.Static Method : Access the static data using class name. Declared inside class with static keyword
3.Abstract Method : abstract methods mean "you have to implement it later point of time" type method ..

Comments
Post a Comment