Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class.getSimpleName() vs hardcoding the class name in java?

Tags:

java

what is the advantage of using Class.getSimpleName() , instead of hardcoding the class name in java?

like image 968
shabarish Avatar asked Feb 11 '23 20:02

shabarish


2 Answers

  • Class.getSimpleName() will always return you the name without any typos, while hardcoding the class name can lead to a typo.

  • If you change the class name, Class.getSimpleName() will return the updated name, while the hardcoded one won't change.

like image 183
Konstantin Yovkov Avatar answered Feb 14 '23 11:02

Konstantin Yovkov


When you re-factor your Class name later, the hardcoded name won't change and the getSimpleName() always gives you the current Class name.

You always need to remember the places to change if you hard code the class name while updating the Class name.

Not only Class name, avoid hard coding AMAP. Otherwise it really increase the cost of maintenance.

like image 37
Suresh Atta Avatar answered Feb 14 '23 09:02

Suresh Atta