3

Can someone explain me the import statements in Java. Some import has * suffixed to it and some doesn't. What is the difference between these two? Does the use of * in the import statement imports all the classes?

see here import

Here they have said that though the import statements seems nested they are not so. Can someone explain in detail?

Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
Swamy
  • 285
  • 1
  • 4
  • 8

4 Answers4

11

The use of * is considered a bad practice. It is used to import all files within that package. The more correct way of doing this is listing off each of the classes that you need, specifically in a scenario where you are doing a code review outside of an IDE and need to know which version of the class you are using. Essentially it breeds laziness in the development team.

Comment

For those arguing that this is not a "bad" practice as I have stated. How can you possibly say that this is a good practice?

import java.util.*;  
import java.io.*;

Even if the compiler ignores everything under the * except the List that you have imported, how does this possibly help someone looking at the code in the future? I think a good deal of people here forget that you are writing code for humans and not the computer. Further how could you possibly convert this code when Java goes away and you are using SuperAwesomeLanguage? Given the following example please convert this to your new language when you have ZERO knowledge of java:

public class Foo  
{
    private List list;
}

Is List in io? is io even required? The issue is you don't know. So by being explicit you can guide future developers as to what classes are required.

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
  • 4
    `The use of * is considered a bad practice` - do you have a source for this? – Perception Feb 15 '13 at 15:05
  • @Perception every project I have ever worked on and every SW engineering class I have ever taken at the undergrad and graduate level. – Woot4Moo Feb 15 '13 at 15:06
  • 2
    The compiler will ignore classes not used. I don't think it's considered "bad practice". – JustinKSU Feb 15 '13 at 15:06
  • @Perception In fact it's clear many people consider it bad practice. Myself I consider it's bad practice to have such a confused code and class name reuses that a `*` import statement could be considered bad practice. Opinion differ... – Denys Séguret Feb 15 '13 at 15:07
  • 4
    Finish your code then ctrl-shift-o :) – JustinKSU Feb 15 '13 at 15:08
  • But what is the difference between these two statements import java.awt.*; import java.awt.event.*; Does it mean that awt and awt.event are different package? This was what said int hat link sir. please clear my confusion. – Swamy Feb 15 '13 at 15:08
  • 2
    @dystroy - I think you hit the nail on the head - its an opinion. Myself, I prefer listing out all packages explicitly, if only to reduce the (slight) chance of simple name conflicts. But thats just a practice of mine and not a prescribed best practice that you will find documented anywhere. – Perception Feb 15 '13 at 15:08
  • In fact it's more often that you let your IDE automatically do the import and that your IDE most often choose the simplest solution : import the class so no conflict can arise. – Denys Séguret Feb 15 '13 at 15:10
  • @user2075925 yes. For instance you can't do this: `import *.*` – Woot4Moo Feb 15 '13 at 15:10
  • @JustinKSU some people develop in notepad so yeah =p – Woot4Moo Feb 15 '13 at 15:11
  • *"How can you possibly say that this is a good practice?"* : I argue that a code so verbose you don't read it and only the computer can parse it is bad code. – Denys Séguret Feb 15 '13 at 15:11
  • here's an example of why it is bad practice: let's say you import util.* and sql.* both packages have a date class. unless you specify the complete package path everytime you instantiate (which means you wouldn't even need an import statement in the first place) the compiler will not understand which date class you want to use. you should always import as specifically as you can. most IDEs will make this simple as they'll pop up a warning when you instantiate and you can click "add import statement" – Jeff Hawthorne Feb 15 '13 at 15:11
  • @dystroy if your import statements are that massive I think it is time to rethink the design choices that have been made. – Woot4Moo Feb 15 '13 at 15:12
  • @Woot4Moo Are you arguing that you don't have classes importing more than 15 classes and that you don't use tools or IDE to help you manage that ? Please... – Denys Séguret Feb 15 '13 at 15:14
  • @dystroy I write my code to be quite reusable. So I make it a point to reduce the growth of the imports. That isn't to say that some libraries such as Spring make that a near impossibility, but when I write pure Java (with no 3rd party libraries except Junit and the Apache suite) my imports are quite minimal. – Woot4Moo Feb 15 '13 at 15:16
  • 2
    @Woot4Moo Saying something is not "bad practice" is not the same as saying it is "good practice". I concede that it can be problematic if conflicts are found. But `import static org.junit.Assert.*;` saves me a lot of time coding... – JustinKSU Feb 15 '13 at 15:31
1

Does the use of * in the import statement imports all the classes

Yes.

From Oracle's Documentation :

A type-import-on-demand declaration allows all accessible types of a named package or type to be imported as needed.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

From your link:

import java.util.*;

The * is a "regular expression operator" that will match any combination of characters. Therefore, this import statement will import everything in java.util. If you have tried entering and running the example program above, you can change the import statement to this one.

So yes * suffix imports all classes in this path

Trudbert
  • 3,178
  • 14
  • 14
1
import com.example.*

Imports all classes in the com.example package

import com.example.ClassName

Imports just the ClassName class

cowls
  • 24,013
  • 8
  • 48
  • 78