File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes import confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "import confusion" Watch "import confusion" New topic
Author

import confusion

colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

whenever I import code, I always use the wildcard eg:

import java.util.*;

but in a lot of people's code, they import a class at a time eg:

import java.util.ArrayList;

what are the advantages and disadvantages to each? I know that importing a whole package doesn't slow the code down at all, so I see no reason to import specific packages.


www.mormon.org
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Importing individual classes allows you to see which classes are in use. It also allows you to see conflicts. For example import java.util.* and javax.swing.* and try to use the Timer class.
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

ah, the API has three timer classes, I could see where that could create conflict, thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
You're welcome
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
colton peterson wrote:what are the advantages and disadvantages to each?


I'm using Eclipse, one of the major free Java IDE's, and one of the very convenient features it has is automatic import management. Eclipse knows exactly which specific imports you need so it tells you when one is missing or when there's one too many. Then you just click on an offer to fix it and it gets done for you.
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

oh, that also makes sense, I'm trying not to use one of the IDE's until I know more about how java works on the inside.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
colton peterson wrote:oh, that also makes sense, I'm trying not to use one of the IDE's until I know more about how java works on the inside.


Well, that's a controversial topic.

If you were a C# programmer and didn't use Visual Studio right from the start you would be considered completely nuts. But for some reason exactly Java programmers think they must start out with Java using programming tools from the sixties before they can graduate to a decent IDE.

I think this is a waste of time really. You don't need to use development tools favoured by your grandpa to get a grip on Java. It's enougth to know that you can develop Java programs from the OS shell using a text editor. You don't have to go through all the pain and actually do it. It won't give you a better understanding of "how Java really works". It's because Java works in different ways depending on which tools you use and no tool-set is closer to the truth than any other. I'd say skip Sun's ancient tools and start using a modern IDE tomorrow.
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

oh I use an IDE, it's called geany, it is a general purpose IDE for a lot of different languages. It doesn't have things like auto import managing, but it is much better than textpad + command prompt
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40

Embla Tingeling wrote:
I think this is a waste of time really. You don't need to use development tools favoured by your grandpa to get a grip on Java. It's enougth to know that you can develop Java programs from the OS shell using a text editor. You don't have to go through all the pain and actually do it. It won't give you a better understanding of "how Java really works". It's because Java works in different ways depending on which tools you use and no tool-set is closer to the truth than any other. I'd say skip Sun's ancient tools and start using a modern IDE tomorrow.


IMHO, a good reason for this is because we have a large number of people going for their certification here at the ranch. And unfortunately, the SCJP does test using how the command line compiler behaves.

In my case, I do use an IDE most of the time, but I also do on-site consulting quite a bit. When you are on-site, you may not get to choose your developement environment, and the command line is the lowest common denominator.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: import confusion