Member Methods


A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions. The purpose of methods is to reuse code: define the code once, and use it many times.

Creating a Method
A method must be declared within a class. It is defined with the name of the method, followed by parentheses ( ).
public class MyClass {
  static void myMethod( ) {
    // code to be executed
  }
}

Java provides some pre-defined methods, such as System.out.println, but you can also create your own methods to perform certain actions. For the above code: Calling a Method
To call a method in Java, write the method’s name followed by two parentheses ( ) and a semicolon (;).

The example uses myMethod to print a text (the action):
public class MyClass {
  public static void main( String[ ] args ) {
    myMethod( );     // Output: "I just got executed!"
  }
  static void myMethod( ) {
    System.out.println( "I just got executed!" );
  }
}




      Did you hear about the mathematician who’s afraid of negative numbers?    
      He’ll stop at nothing to avoid them.