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().toString();
    }

}

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()

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap