Post 2: Understanding Methods in Java

A method is a block of code that performs a specific task.
Structure of the Method

public returnType methodName(parameterType parameterName) {
// Method body (code to be executed)
}

Let’s break down this

public: public is the Access Modifier….


This content originally appeared on DEV Community and was authored by Sadaf Khan

A method is a block of code that performs a specific task.
Structure of the Method

public returnType methodName(parameterType parameterName) {
// Method body (code to be executed)
}

Let's break down this

  • public: public is the Access Modifier. Whether you want to show it to the other or not. So here 'public' means that you want to show it to others. There are different types of Access Modifiers e.g., public, private, protected.

  • returnType: It is what the method will return. If the method does not return anything then you type void.

  • methodName: This is the name you give to the method, which should be descriptive of what the method does.

Image description

In method 3("named as multiply") we assign the parameters in the parentheses after the method name because we want input from the users.


This content originally appeared on DEV Community and was authored by Sadaf Khan


Print Share Comment Cite Upload Translate Updates
APA

Sadaf Khan | Sciencx (2024-10-02T00:15:15+00:00) Post 2: Understanding Methods in Java. Retrieved from https://www.scien.cx/2024/10/02/post-2-understanding-methods-in-java/

MLA
" » Post 2: Understanding Methods in Java." Sadaf Khan | Sciencx - Wednesday October 2, 2024, https://www.scien.cx/2024/10/02/post-2-understanding-methods-in-java/
HARVARD
Sadaf Khan | Sciencx Wednesday October 2, 2024 » Post 2: Understanding Methods in Java., viewed ,<https://www.scien.cx/2024/10/02/post-2-understanding-methods-in-java/>
VANCOUVER
Sadaf Khan | Sciencx - » Post 2: Understanding Methods in Java. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/02/post-2-understanding-methods-in-java/
CHICAGO
" » Post 2: Understanding Methods in Java." Sadaf Khan | Sciencx - Accessed . https://www.scien.cx/2024/10/02/post-2-understanding-methods-in-java/
IEEE
" » Post 2: Understanding Methods in Java." Sadaf Khan | Sciencx [Online]. Available: https://www.scien.cx/2024/10/02/post-2-understanding-methods-in-java/. [Accessed: ]
rf:citation
» Post 2: Understanding Methods in Java | Sadaf Khan | Sciencx | https://www.scien.cx/2024/10/02/post-2-understanding-methods-in-java/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.