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 statement - Difference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "import statement - Difference" Watch "import statement - Difference" New topic
Author

import statement - Difference

Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
If it is a static import then

makes sense.

If is a normal "import" statement then is there any difference between


What does import java.lang.String.*; signifies
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Naresh Shanmugam wrote: . . . What does import java.lang.String.*; signifies
Have you tried it? Did it compile?
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
Yes I tried Campbell, No compilation errors
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Hmm, I didn't think it would compile.

In any case, to use a better example:won't import the Element class, because it thinks you're specifying the name of a package. Or at least that's what I would have said before discovering that would actually compile.

I think this section explains why it compiles, but I'm not sure what it actually *means*:

http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.5.2
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
I didn't expect it to compile either. Having looked at that JLS link you quoted, David, it would appear that we have imported all inner/nested classes inside String.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
I earlier wrote: . . . imported all inner/nested classes inside String.
There don't seem to be any inner classes in String, but the compiler seems happy with an on-demand import of an empty package.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That's the only interpretation I could come up with, but I'm left a little unsatisfied... just like my import statement should be :D
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Execute thisand that should confirm whether we are right or not.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

So I guess we were right... but... well, I guess there's nothing that says you can't import an empty package.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
No, that's a bit like teaching our undergraduates whatmeans, where boo is any Boolean value.
Cody Long
Ranch Hand

Joined: Jan 01, 2009
Posts: 95
Naresh Shanmugam wrote:If it is a static import then

makes sense.

If is a normal "import" statement then is there any difference between


What does import java.lang.String.*; signifies


Imports all of the classes in the package String. Which, as it has been said, I did not think that this would compile.

Imports just the String package. If you wanted to use any of the classes in the package String then you would have to specify 'Swing.' before the class name.

A better example would *might* have been:

If you wanted to use any of the classes in swing when you use the import statement without the '.*' then you have to specify 'swing.' before any of the class names you are using.

For example:


I hope i cleared things up for you.

EDIT: replaced 'would' with '*might*'


Duct tape is like the Force. It has a dark side, a light side, and it binds the whole universe together.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm not convinced that's a better example, though, since the question explicitly asked about a star import against a class.
Cody Long
Ranch Hand

Joined: Jan 01, 2009
Posts: 95
David Newton wrote:I'm not convinced that's a better example, though, since the question explicitly asked about a star import against a class.

Ok, i see what you mean. I edited my post to replace 'would' with 'might'.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: import statement - Difference
 
Similar Threads
importing packet
import problem
Object reference not working with Bean method
Document Listener and/or Ley Listener
Concatenation of null keyword to a String