Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2004
    Posts
    70

    Unanswered: Beginner java question. "constant expression required"

    Im having to compile some old java code with SDK 1.4.2. I ran into a bunch of switch statements that looked like this:

    Code:
    public class MyClass
    {
            public static final int Var1		= 0;
            public static final int Var2		= 1;
            public static final int Var3		= 2;
    
            public static final String TXT_Var1	= "Name of Variable 1";
            public static final String TXT_Var2	= "Name of Variable 2";
            public static final String TXT_Var3 	= "Name of Variable 3";
    
    	public String getVariableName(int columnIndex)
    	{
    
    		String result = "??";
    		switch(columnIndex)
    		{
    			case this.Var1: 		result = this.TXT_Var1;
    			break;
    			case this.Var2: 	result = this.TXT_Var2;
    			break;
    			case this.Var3: 	result = this.TXT_Var3;
    			break;
    		}
    		return result;
    	}
    }
    This would give "constant expression required" on the "case this.Var1" var2 var3 lines.

    If I change the code to this:

    Code:
    public class MyClass
    {
            public static final int Var1		= 0;
            public static final int Var2		= 1;
            public static final int Var3		= 2;
    
            public static final String TXT_Var1	= "Name of Variable 1";
            public static final String TXT_Var2	= "Name of Variable 2";
            public static final String TXT_Var3 	= "Name of Variable 3";
    
    	public String getVariableName(int columnIndex)
    	{
    
    		String result = "??";
    		switch(columnIndex)
    		{
    			case MyClass.Var1: 		result = this.TXT_Var1;
    			break;
    			case MyClass.Var2: 	result = this.TXT_Var2;
    			break;
    			case MyClass.Var3: 	result = this.TXT_Var3;
    			break;
    		}
    		return result;
    	}
    }
    it compiles fine.

    Is replacing "this" with the class name acceptable? No way I can test the actual code right now so Im curious how I am supposed to fix this.

    Thanks

    edit----

    hrmmm or am I just supposed to do this?

    Code:
    public class MyClass
    {
            public static final int Var1		= 0;
            public static final int Var2		= 1;
            public static final int Var3		= 2;
    
            public static final String TXT_Var1	= "Name of Variable 1";
            public static final String TXT_Var2	= "Name of Variable 2";
            public static final String TXT_Var3 	= "Name of Variable 3";
    
    	public String getVariableName(int columnIndex)
    	{
    
    		String result = "??";
    		switch(columnIndex)
    		{
    			case Var1: 		result = this.TXT_Var1;
    			break;
    			case Var2: 	result = this.TXT_Var2;
    			break;
    			case Var3: 	result = this.TXT_Var3;
    			break;
    		}
    		return result;
    	}
    }
    Last edited by JamesAvery22; 11-04-04 at 18:23.

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    The switch label (that is, what's after "case") must be known at compile time. "this" refers to an instance of the class, which only exists at run time, so technically whatever comes after "this" may not be known during compilation. Some implementations of the compiler may be "smart" enough to look ahead and see that what you're actually referring to is a static member.

    On the other hand, MyClass.Var1 is known at compile time since it refers to a static member that's initialized and "final".

  3. #3
    Join Date
    Jan 2004
    Posts
    70
    So is doing the MyClass.Var1 ok?

  4. #4
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    In my view, it's not only OK but the right way to do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •