Java: import vs static import

preview_player
Показать описание
* What is the difference between import and static import in Java?
* What is the advantage and disadvantage of static import?

Рекомендации по теме
Комментарии
Автор

Damn man, you have chosen a troll day to upload and now i am asking myself if this is a troll video at 0:35 and if i should keep watching im running out of time xD

wizardchris
Автор

There is your like. This covers the controversy very well. Advantage: Less typing to do. Disadvantage: Someone who isn't in that code all that time may be unsure about where the methods are coming from when they look at the code, if the class name was reasonably short maybe it would be better to have it for documentation.
My personal take-away, always use it for the very common static methods of very common static classes we are all using all the time -- if someone doesn't know Java they will have a hard time reading our code anyway. In a particular codebase, if there are very common static methods on very commonly used classes, same thing, people will quickly get used to them and it is just wasting space and creating noise.
Automatically doing it all the time for even very rarely used static methods of very rarely used classes is not such a great idea, yes, it makes the code shorter but so would making all class method and variable names one character each -- you are hurting the readability of your code at that point, and will need to use IDE features to make sense of anything. This was the case for a long time in big projects in C, so I am sensitive to times it feels like we are moving backwards. I was obviously attracted to this video by you asking the musical question of "What is the advantage and disadvantage?" I wanted to see if you felt the same way I did.

jvsnyc