Note: I realize that this is probably an open-ended question, so go ahead and flag for deletion if you feel this either doesn’t belong or has no merit.
I haven’t been working with Java (or really programming in general) for very long and most of my time is spent on learning practical application (specifically with Android). Therefore, I realize that I am very behind when it comes to conceptual issues.
My question is this: When is it appropriate to use the import 'package name'; statement instead of simply using the fully qualified class name?
I’ve seen sample code where the coder decided to use android.widget.Button vs. calling import android.widget.Button and using Button for every instance afterwards. I’m currently writing code where I know I will be using a certain class only once and I feel like using an import statement is a waste.
Any guidelines on when I should be using an import statement vs. using a fully qualified name would be much appreciated. I am not looking for a definitive answer, just some helpful tips.
The solution is simple: if you have more than 1
Buttonclass (obviously, each in their own package) that you have to use in the same class, then use the fully qualified name. Otherwise, import the onlyButtonclass that you need.Generally, package import is preferred as it brings cleaner code and it makes compiler knows what class (package) to compile upfront.