Allan Didier

Void vs Non-Void Methods

Question

What does Void mean? What are Void methods versus non-Void methods?

Answer

Void methods will do something but will not return anything. “Setter” methods are void methods.

Non-Void methods will return a value. They can return an int, float, boolean, double, String, array or objects. “Getter” and the “toString” methods are non-void methods because they return a value. Non-Void methods must have a “return” statement in them. 

Example

public class MethodTypes
{
    private String name;
    private int value;
    private ArrayList <String> ingredients;
 
    // void methods
    public void setName (String newName) {
        this.name = newName;
    }
 
    public void setName (String newName) {
        this.name = newName;
    }
 
    public void setName (String newName) {
        this.name = newName;
    }
 
 
    // non-void methods
    public String getName () {
        return this.name;
 
    public int getValue () {
        return this.value;
 
    public ArrayList <String> getIngredients () {
        return this.ingredients;
 
    public boolean containsIngredient (String ingredient) {
        return this.ingredients.contains(ingredient);
}

Other Resources

Lesson 34: Void vs non-void methods