Holger Kibur
Holger Kibur

Reputation: 47

Why cant I just import everything?

I love Java. I have heard a lot of bad things about java on how it is slow and all, but for me, it seems like a better alternative to C and C++. The only reason that I don't like java is because the standard library is SO BIG and it takes forever to just set up all of the import statements in my code. Why can't I just do

import java.*;

and have all of my import statements in one neat package. I am not making a big program so speed is not an issue. Thank you!

Upvotes: 1

Views: 185

Answers (2)

Mark Bordelon
Mark Bordelon

Reputation: 123

There are quite a few classes (especially common or basic classes) that have the same name but are parts of different packages. They are thus different code, doing different things, or the same things in different ways. Consider the Hibernate and JPA entity classes as just one of many examples. If you imported everything, you would have no way to control which class is actually compiled and used. Using an IDE, like so many are recommending, lets you see the possibilities and choose the one you really need (and the IDE will then usually automatically write the import statement for you). On the flip side, there is nothing wrong with fully qualifying all your classes. It just is wordy and not elegant. So you see that the import statements don't really import anything, they are just conveniences to avoid having to prepend all your classes with packages.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

Import statements are only for the aid of the developer, at runtime they've been used to fully qualify the classes anyway. Further, it's very similar to the namespace feature in C++ and you wouldn't want to make everything a global there either (although Java does effectively import java.lang.* by default). Finally, a free IDE like eclipse or Netbeans will help manage import(s) for you.

Upvotes: 3

Related Questions