• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between static import and normal import.  RSS feed

 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks!,

I just found following code snippets in one of my projects.




what is the meaning of following import?


It seems like i just imported a method instead for class.

Note : this works fine even we remove entire import statement.

Thanks in advance!

-Supun
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you have imported a method, not a class. But you have imported something which was already a member of that class, because public static members are inherited by subclasses. So the import was unnecessary. Remember you should not try to override a static method.
 
Safi Khan
Greenhorn
Posts: 19
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static import should be used when you want to import only static members or methods of the class. However there is no significant performance difference between the two imports.

Note : this works fine even we remove entire import statement.


Because both classes are in the same package i.e com.supun.sample; so, when classes are in same package you don't need to write explicit import statement.

Regards,
SDK
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would still need a static import even if the classes are in the same package.
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys!
Thank you very much!

I didn't knew that we can import method.

again, thanks!
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can: look at these links: 1 2 3.
I look on it as static import permitting you to pretend that a public static member of another class is a private static member of this class.
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Yes, you can: look at these links: 1 2 3.
I look on it as static import permitting you to pretend that a public static member of another class is a private static member of this class.


Thanks! very useful!
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Safi Khan wrote:Static import should be used when you want to import only static members or methods of the class. However there is no significant performance difference between the two imports.


Thanks!
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You would still need a static import even if the classes are in the same package.


Why? there is no need of that.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, how are you going to use ZERO in class Bar. You either have to use the old‑fashioned technique of writing Foo.ZERO, or write plain simple ZERO, which requires a static import.

At least I think it does; I have never tried. Try it and tell us what happens.
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: . . .
So, how are you going to use ZERO in class Bar. You either have to use the old‑fashioned technique of writing Foo.ZERO, or write plain simple ZERO, which requires a static import.

At least I think it does; I have never tried. Try it and tell us what happens.


Yeah that works. but in my problem so no need of static import.

anyway thanks for your help.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Different problem if you are extending a class. But why should you extend a class with public fields? If you have a class to encapsulate constants, it is like a utility class, which should be uninstantiable; you cannot therefore inherit from it.

And if you have public non‑constant fields, those are global variables, which is poor practice.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Supun Lakshan Dissanayake wrote: . . . thanks for your help.
You're welcome

I have shortened the quote in your post; there is usually no need to quote the whole of a previous post.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!