YoMama YoMama - 8 days ago 6
Java Question

Void is an invalid type for the variable (JAVA)

I am getting the error, void is an invalid type for the variable Divide. Hopeful someone could help.

class Star {
public static void main(String args[]){

Divide(2);


public void Divide(int z){
try{
System.out.println(z/0);
}

catch(ArithmeticException e )
{
System.out.println("Can't do that!");
}

}
}
}

Answer

You cannot call a non-static method (Divide) from a static method (main). In this case I'd make the divide method static since your class does not have state anyway. You should also make the name divide instead of Divide to follow the Java coding conventions.

public class Star {
    public static void main(String[] args) {
        divide(2);
    }

    public static void divide(int z) {
        try {
            System.out.println(z/0);
        }
        catch(ArithmeticException e ) {
            System.out.println("Can't do that!");
        } 
    }     
} 
Comments