How to Create a Method in Java
A method is a unit of code that you call in a Java program. A method can support arguments and usually returns a value. To create a method in Java, follow these four steps.
- Open your text editor and create a new file. Type in the following Java statements:
The method you have coded is named
computeAreaOfRectangle. It accepts two integer arguments and returns an integer, the product of the two arguments.
- Save your file as CreateAMethodInJava.java.
- Open a command prompt and navigate to the directory containing your new Java program. Then type in the command to compile your program and hit Enter.
- You can now test your Java program. Type in the command to run the Java runtime launcher and hit Enter. Observe the results of calling your method.
Author: Stephen Withrow