Fix: java: void cannot be dereferenced

Example Code:
public class Example {
    
    static void sayHello() {
        System.out.println("Hello!");
    }

    public static void main(String[] args) {
        sayHello().<span style="color:red">toString()</span>;
    }

}

Error:

java: void cannot be dereferenced


Reason for the error:

As you can see we get an error "void cannot be dereferenced" when we try to access a member on a void type. In the above example sayHello() returns void and we are trying to access toString()

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!