Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import statement - Difference

 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naresh Shanmugam wrote: . . . What does import java.lang.String.*; signifies
Have you tried it? Did it compile?
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I tried Campbell, No compilation errors
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Execute thisand that should confirm whether we are right or not.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's a bit like teaching our undergraduates whatmeans, where boo is any Boolean value.
 
Cody Long
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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*'
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic