4.4 Declaring Methods 

Previous Index Next 


Methods are declared in the following way in Java:
 
MethodModifiers ResultType Identifier ( [ParameterList] ) [throws ExceptionList]
where The following is some examples of method declarations:
public class MyMethods
 {
   protected static long Counter = 0;  // declare static counter set to 0

   public void procOne()               // simply method no parameters, no result.
    {
      //...code here
    }

   public int addNumbers(int numberOne, int numberTwo) //  methods takes to integers, adds them and returns result
    {
     return numberOne + numberTwo;
    }

   public static synchronized long getCounter() // thread safe method returns counter. Can be called on class
    {
     return counter;
    }
 }


Sources