Google News
logo
Java Constructor - Interview Questions
What happens if you keep a return type for a constructor?
Ideally, Constructor must not have a return type. By definition, if a method has a return type, it’s not a constructor.(JLS8.8 Declaration) It will be treated as a normal method. But compiler gives a warning saying that method has a constructor name.

Example :
class FTL
{
    int FTL()
    {
        return 0;    // Warning for the return type
    }
}​

 

Advertisement