In Java 5, static imports allow you to take a definition from an interface and conveniently use it as if it is a locally defined variable.
If you must use static imports, make sure your namespace is unique enough.
Bad:
package com.xyz.photos.web;
// Defining some view names
public interface ViewNames
{
String INVITATION_ALBUM = "invitationAlbumView";
String INVITATION_ALBUMS = "invitationAlbumsView";
String NO_ALBUMS = "noAlbumsView";
String NO_PHOTOS = "noPhotosView";
}
import static com.xyz.photos.web.ViewNames.*;
:
if (isInvitation)
{
// Using the view name we defined earlier
return new ModelAndView(NO_PHOTOS);
}
:
In the above example, the NO_PHOTOS was not unique and could clash with another variable. Also, you can't tell where it is defined without hovering the mouse over the code. A better naming convention would be to add VIEW_ before the variable to show where it came from:
Good:
package com.xyz.photos.web;
public interface ViewNames
{
String VIEW_INVITATION_ALBUM = "invitationAlbumView";
String VIEW_INVITATION_ALBUMS = "invitationAlbumsView";
String VIEW_NO_ALBUMS = "noAlbumsView";
String VIEW_NO_PHOTOS = "noPhotosView";
}
import static com.xyz.photos.web.ViewNames.*;
:
if (isInvitation)
{
return new ModelAndView(VIEW_NO_PHOTOS);
}
: