aspose file tools*
The moose likes Beginning Java and the fly likes Static imports Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static imports" Watch "Static imports" New topic
Author

Static imports

Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Aug 31, 2012
Posts: 127
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
Bartender

Joined: Oct 14, 2005
Posts: 18643
    
    8

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

Joined: Aug 31, 2012
Posts: 127
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

Joined: Oct 13, 2005
Posts: 39388
    
  28
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

Joined: Oct 31, 2012
Posts: 38

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static imports