How do you create methods | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

How do you create methods

java

8th Nov 2017, 4:41 PM
Anu
3 Answers
+ 6
Also, it's worth noting the structure of a method: <modifier> <return type> <method name> (<parameters>) <exception lists> { // method body } Modifiers help you define the scope of your method, such as if it's public or private. Return type is the data type that will be returned to whatever called the method. For example, int will return an int value, void doesn't return anything, and String would return a string value. Method name is simply whatever you wish to call the method. Parameters are the arguments that methods can take. So if you're adding two numbers you may have sumItUp(int num1, int num 2) which would translate to something like sumItUp(5, 10) or sumItUp(numInput1, numInput2). Basically, you're feeding data from where the method is being called so that it can be used inside of the method's scope for calculations or such things. The exceptions list helps out in regard to throwing exceptions with the code.
8th Nov 2017, 5:02 PM
AgentSmith
+ 2
public int abc() { return int; }
8th Nov 2017, 4:43 PM
shobhit
shobhit - avatar
0
thanks
8th Nov 2017, 4:49 PM
Anu