Reputation: 181
I tried finding the answer to this in The 11th Edition of the Java complete reference, but didn't find the answer. I am creating a Java Package. I am using intelliJ for IDE and MAVEN as the build tool. Within that Java Package, there are multiple classes. Within each class I have various imports. For example, I have:
import java.util.*;
(i) Am I correct that I need this import at the start of EACH class and can't rely on the fact that I import it in ONE of the classes within the package?
(ii) If so, does MAVEN somehow avoid duplicating the imports?
(iii) If not, then doesn't that create an incredible inefficiency vis a vis memory?
Upvotes: 0
Views: 465
Reputation: 77234
The import
statement applies to the file in which it appears. That could be more than one class, but in vanilla Java it includes exactly one top-level type.
Maven has nothing to do with import
, which is merely a shortcut so that you don't have to say
public java.util.Map<java.lang.String, com.example.app.model.Person>
groupByDepartment(java.util.Collection<com.example.app.model.Person> people)
Maven simply ensures that the library jars you need are downloaded and activated.
Importing doesn't "do" anything except let you use the short names of types (or members, for static imports). That's all. The compiler replaces these with the full names anyway. There's no appreciable memory overhead, and zero at runtime.
Upvotes: 2