Please enable JavaScript to use CodeHS

CodeHS Glossary


Define a method Java

Defining a method means to teach the computer a new command and explain what it should do when receiving that command. Defining a method does not run any of the code inside that method. It only teaches the computer what that method means. You can actually run that code by “calling” the method. **Example** The way you define a method in Java is: ``` private void nameOfMyMethod() { // method body: the code for this method goes here } ``` The method definition starts with `private void` followed by the name of the method. At the end of the name, be sure to include the () to complete it. All of the code for the method goes in between the { and } That section is called the “method body.” **Defining vs. Calling** Defining a method is different from “calling a method.” When you “call” a method, you just write the name of the method followed by `();` like this: ``` nameOfMyMethod(); ``` This will actually run the code inside that method, whereas defining the method does not run the code. It just defines the new command. **Teaching Karel a New Word** In Karel, you can think of defining a method as teaching Karel a new word. You can teach Karel any new word you want using and of the other words that Karel already know, including other words you have taught Karel.