Please check why my java code throws up with a syntax error

Please check why my code throws up an error -

Programming language = Java.


public class HelloWorld{
    
    int Number;
    boolean Ispossible;
    
public sayhello (int Num, boolean possible) {
    Number = Num;
    Ispossible = possible;
};
     public static void main(String []args){
        System.out.println("Hello World");
     }
};

It doesn’t works here is the syntax error =


Store.java:7: error: invalid method declaration; return type required
public sayhello(int Num, boolean possible) {
^
1 error


the error message helps a lot:

invalid method declaration; return type required

Java is static type, which means we need to explicitly tell the return type of a method, in generic fashion we then have:

<access modifier> <return type> <method name> 

you have the access modifier (public), the method name (sayhello). but the return type is missing:

public .... sayhello (int Num, boolean possible) {
    Number = Num;
    Ispossible = possible;
};

also, sayHello seems to be your constructor, which means it should have the same name as the class. Otherwise a method should preferable be cammelCase.

Thanks sir. It helped a lot!

i am new to java… does that mean we can’t create classes of our own names in java?

We can name our classes, but the constructor (which is called when you instantiate an class) needs to be the same name as the class. But you are free to choice the class name