Method Mark and Lyubo
Purpose to break down larger tasks into smaller ones to organize code
Declaring a method public static void drawBar (int length) { <return type> <parameters> <name> public static void drawBar (int length) { System.out.print("*"); } <access level> <statements>
Access level determines if the method can be called by other classes is either public or private Ex: public static void drawBar (int length)
Return type Declares if the method will return a value Ex: double, char, and int void will not return a value Ex: public static void drawBar (int length)
Name the name of the method starts with a lowercase and if there is more words the following words will be uppercase in no case do you add spaces public static void drawBar (int length)
Parameters values are used in a method parameters are enclosed by brackets parameters must be separated with commas Ex: public static void drawBar (int length)
Method declaration the keyword static in the method declaration makes the method able to be called from the class it is in Ex: public static void sumNum (int num1, int num2) { } If the return type is not void the return statement must be in the method
types of parameters types of parameters: formal and actual Ex: public void setText (String text) setText(“bob”); formal parameter actual parameter
Return Statement used when return type is other than void is specified in the method declaration return statement sends a value back to the method call can’t contain more than 1 value Ex: public static int areaOf(int size) { int area; area = size * size; return(area); }