What happens if you keep return type for a constructor?

As we discussed in previous questions that we can overload a constructor so if we keep return type for a constructor it will be treated as a normal method.

Note: Compiler gives a warning message that method has a constructor name.

Example

public class Main
{
    String Main(){
        System.out.println("Hello World");
        return "Hello W3spoint.com";
    }
	public static void main(String[] args) {
		Main object = new Main();
		 System.out.println(object.Main());
	}
}

Output

Hello World 
Hello W3spoint.com

Java interview questions on constructor

Please follow and like us:
Content Protection by DMCA.com