• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static imports

 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also i would like to ask why is there any need to use static import ?

eg. import static java.util.Collections.*;

Can't we import, static members using
import java.util.collections.*;
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's never necessary. Even normal imports are never necessary. They're just convenient. They save typing and can leave your code less cluttered.

A lot of people don't like static imports, because they feel it makes it look like something is part of the current class when it's not. Using them is a trade of between being brief vs. being precise.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:It's never necessary. Even normal imports are never necessary. They're just convenient. They save typing and can leave your code less cluttered.

A lot of people don't like static imports, because they feel it makes it look like something is part of the current class when it's not. Using them is a trade of between being brief vs. being precise.


Ok thanks, i got it.
 
Paul Clapham
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used static import for an enum, so that I don't have to prefix the enum constants by their class name. But even that is questionable, like Jeff said. I very much doubt I would use it for anything else.

Although... it might be nice to static-import the Math class, so I could write "sin" instead of "Math.sin" and so on. I might even be tempted by that. Good thing I don't ever have to use trigonometry in my Java programming.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I have used static import for an enum, so that I don't have to prefix the enum constants by their class name. But even that is questionable, like Jeff said. I very much doubt I would use it for anything else.

Although... it might be nice to static-import the Math class, so I could write "sin" instead of "Math.sin" and so on. I might even be tempted by that. Good thing I don't ever have to use trigonometry in my Java programming.


Yes now i will also use simple import without static as i think it's not that necessary.Although using it with enums would be a good idea.
 
Campbell Ritchie
Sheriff
Posts: 48370
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use static import as much or as little as you like. If you get any confusion about what foo() means, you have used static import too much.
 
Rahul mir
Ranch Hand
Posts: 38
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to access static members, it is necessary to qualify references with the class they came from. For example, one must say:
double a = Math.sin(Math.PI * theta);

If the frequency of access of static member is more in program, some people like to use static import , just for there time saving.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic