Raj
Raj

Reputation: 1

Why cannot we have constructor with name different from class names as long as they are unique

I understand that Java imposes a restriction that class name should be same as constructor name. Why is this restriction imposed? Why cannot I have a constructor of the class with a different name than class name as long as no constructor with the same name exists for any other class?

Upvotes: 0

Views: 1896

Answers (4)

David Soroko
David Soroko

Reputation: 9086

I guess the convention of having the same name for the class and it's construcors was considered useful. Alternatively the language would have to be extended with a keyword ( constructor ?), method name conflicts resolved etc...

Can you share a use case that benefits from arbitrary named construcor method?

Upvotes: 0

Kartik Gautam
Kartik Gautam

Reputation: 128

This convention is for programming ease, constructor chaining and consistency in the language.

For example, consider a scenario where you want to use Scanner class, now what if the JAVA developers named the constructor as xyz!

Then how will you get to know that you need to write :

Scanner scObj = new xyz(System.in) ;

which could've have been really weird, right! Or, rather you might have to reference a huge manual to check constructor name of each class so as to get object created, which is again meaningless if you could have a solution of the problem by just naming constructors same as that of the class.

Secondly, the constructor is itself created by the compiler if you don't explicitly provide it, then what could be the best name for constructor could automatically be chosen by the compiler so it is clear to the programmer! Obviously, the best choice is to keep it the same as that of the class.

Thirdly, you may have heard of constructor chaining, then while chaining the calls among the constructors, how the compiler will know what name you have given to the constructor of the chained class! Obviously, the solution to the problem is again same, KEEP NAME OF THE CONSTRUCTOR SAME AS THAT OF THE CLASS.

Thanks for Asking.

Upvotes: 0

Neel Patel
Neel Patel

Reputation: 143

Let's assume that we can have another name rather than the class name, You have to check whether it's already been used for any other class or not. To elaborate more consider the following example,

Person p1 = new Vehicle(); 

Now, In another class

MotorCycle m1 = new Vehicle(); // if you have used the same name then it will create a conflict for the compiler which implementation should be considered. 

Now, think about the actual constructor naming conventions that if you use the same as for Class name, it's easier and also makes sense.

Person p1 = new Person();
MotorCycle m1 = new MotorCycle();

Upvotes: 1

Keval
Keval

Reputation: 1602

This is because Java cannot "see" other classes, it only cares about the one class when it's compiling it.

Also, if the constructor is named something other than the class name, it cannot be found and it doesn't make any sense anyway. Imagine doing this:

Person person = new Robot();

where Robot would be a constructor in Person. It just doesn't make any sense to humans or the compiler!

Upvotes: 0

Related Questions