Syntax Quiz 2, Step 4

Assignment:

Write a method definition for a method named doSomething that returns nothing, takes no parameters, and does nothing.

My solution:

public void doSomething ()
   {
   } 
(Note that the "public" is optional.)

Common mistakes:

Omitting the return type.
A method definition must have a return type, before the name of the method. If the method returns nothing, it must have return type void.
Putting the method name in quotation marks.
A method name, like a variable name or a class name, cannot contain quotation marks.
Omitting the parentheses.
Even if there are no parameters to the method, it must have a pair of parentheses after the method name so Java knows you're defining a method rather than a variable.
Omitting the curly-braces.
A method definition can be broken into a method header and a method body. The method header consists of an optional public or private, a return type, a method name, and a pair of parentheses which may contain parameter declarations. The method body is enclosed in curly-braces after the method header.
Putting a semicolon after the method header.
A method header is not a statement, so it doesn't end with a semicolon.

Last modified:
Stephen Bloch / sbloch@adelphi.edu