Chapter 8. Static Imports
Note
In this chapter:
All right, let’s begin with a fair warning—this isn’t a very sexy chapter.
It’s not as exciting as generics, it’s not as (obviously) missing a feature in
the language as varargs, it’s not even as cool as the for/in loop. Instead,
this chapter is about a feature that is completely convenience-based. It
adds no new functionality to the language, and it doesn’t even have
much to do with how you write code. Sort of an inauspicious beginning,
huh?
Still, that said, static imports are extremely cool, especially when you start to pile on additional language features, such as enumerated types. In a nutshell, static imports allow you to import static classes, variables, and enums, and reference them easily in your code. While you may not get any new functionality, the convenience here is very, very nice. As I’ve said before, I don’t know any good programmer who just loves typing out really long variable names, ten or fifteen times in their programs. Static imports help with just that task, and, as such, are very much worth covering.
Importing Static Members
If you’ve ever done any output
in Java, you’ve typed System.out at least
a few times. While there are lots of better ways to handle output, there is perhaps none as simple, direct, and to-the-point as good old System.out.println( ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access