Recently i cam across a statements :
import static java.lang.System.out;
import static java.lang.System.exit;
I read these statements in some tutorial. Are these statements O.K ?
If the statements are alright what do they mean and should they be used regularly while writing code ?
They are called static imports. The effect is to allow you to use the names
outandexitin your program as if they were defined in the current scope; so you can writeexit(0)instead ofSystem.exit(0).Now, are they a good idea? Sometimes, when used sparingly, they are a good way to reduce clutter. But most of the time, they actually just make your code harder to understand. The reader will ask “Where is this
outdefined?” and “Where doesexit()come from?” In general, you should avoid them.But if you’re writing a class that’s all about processing
SomeReallyLongNameobjects, andSomeReallyLongNamedefines a bunch ofFINAL_CONSTANTS, importing them with static imports will save a lot of typing and a lot of clutter, and it will be pretty clear where those constants are coming from.