Microsoft KB Archive/325740

= BUG: Inner Compile-Time Constant String Members Not Permitted =

Article ID: 325740

Article Last Modified on 1/19/2004

-

APPLIES TO


 * Microsoft Visual J++ 6.0 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition

-



This article was previously published under Q325740



SYMPTOMS
When you define a static final string member in an inner class, such as the following public class Outer { private class Inner {   static final String  NAME = &quot;Bob&quot;; } } you may receive the following error messages in the Visual J# .NET compiler:

error VJS1176: Cannot declare field 'NAME' static in an inner class

-or-

error VJS1175: Inner class 'Inner' contains a static initializer



RESOLUTION
Future versions of the Visual J# .NET compiler are expected to support static final string member types in inner classes. See the &quot;More Information&quot; section for possible functional equivalent workarounds.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



MORE INFORMATION
The Java language specification permits compile-time primitive type constants in inner classes, including string literals. However, neither the Visual J++ 6.0 nor the Visual J# .NET compilers support this feature.

To work around the problem, do not use the static modifier, and use instance variables for string constants in inner classes. You can also define static string constants in an interface that can then be exposed on the inner class. The following is a code sample for both scenarios: public class OuterClass { public interface IStringConstant {   String NAME = &quot;Robert&quot;; }

private class Inner implements IStringConstant {   final String name = &quot;Bob&quot;; }

public static void main(String[] args) {   System.out.println(&quot;static final string on interface:  &quot;       + Inner.NAME);

System.out.println(&quot;final instance string on class: &quot;       + new OuterClass.new Inner.name); } }

