[Solved] Override toString() method java


why output is 0 not “lol” ?

because you are printing an integer and not an instance of that Main class

you can do the following

public class Main   {

    @Override
    public String toString(){
        return "lol";
    }

    public static void main(String[] args) {
         // int aaa=0;
        Main myMain = new Main();
        System.out.println(myMain);
    }
}

note that you can do

System.out.println(myMain);

the same as

System.out.println(myMain.toString());

solved Override toString() method java