ClassgetSimpleName () vs hardcoding class name in java?

What is the advantage of using Class.getSimpleName()

instead of hardcoding the class name in java?

+3


source to share


3 answers


  • Class.getSimpleName()

    will always return you the name without any typos , whereas hardcoding the class name can lead to a typo.

  • If you change the name of the class Class.getSimpleName()

    will return the updated name and the hardcoded one will not change.



+6


source


If you redefine your class name later, the hardcoded name will not change, but getSimpleName()

will always indicate the current class name.

You should always remember the places to change if you are hard-coding the class name when updating the class name.



Not just the Class Name, avoid hardcoding AMAP. Otherwise, it really increases the cost of maintenance.

+2


source


java.lang.Class.getSimpleName () returns the simple name of the base class as given in the source code. Returns an empty string if the base class is anonymous.

since Kocko said that Class.getSimpleName () will always return the name to you without any typos, while you can hardcoding the typo on the class name. "and as sα΄œΚ€α΄‡sʜ α΄€α΄›α΄›α΄€ said," When you rephase your class name later, the hardcoded name will not change and getSimpleName () always gives you the current class name. "so you can keep making changes and refracting the class without worrying about creating an error in the dependent classes.

0


source







All Articles