Literal values use in Eclipse


If you have experienced following comment in your code, I probably assume you are an Eclipse developer.

log.warn("Could not save file " ); //$NON-NLS-1$

Those comments silence a warning that Eclipse emits when it encounters string literals (and has been configured to complain).

The idea is that UI messages should not be embedded as string literals, but rather sourced from a resource file (so that they can be translated, proofed, etc). Consequently, Eclipse can be configured to detect string literals, so that you don’t accidentally have leave unexternalized UI strings in the code; however, there are strings which should not be externalized (such as regexps) and so, //$NON-NLS-1$ gives you a way to communicate that fact to the compiler.

Best practice to use literal values is using and files.


import org.eclipse.osgi.util.NLS;
public class Messages extends NLS { 
  static {
   NLS.initializeMessages("messages",Messages.class); //$NON-NLS-1$
  private Messages() {
  public static String MessageNeedToUse;

In file

MessageNeedToUse = Could not save file

Now you can use above literal with Messages class.






Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s