- 1
How do you create methods
java
3 Antworten
+ 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.
+ 2
public int abc() {
return int;
}
0
thanks