Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.
public interface Constants{
    String TEST = 'test';

}

I am getting below error -

Error: Compile Error: expecting a left parentheses, found '=' at line 2 column 16   

Can someone explain why am i getting this error?

share|improve this question
3  
You may have thought you could do that because Java supports constants in interfaces, but Apex does not. Depending on your use case you might want to consider Custom Labels or Custom Settings –  Peter Knolle Dec 8 '12 at 12:42

2 Answers 2

up vote 6 down vote accepted

Interfaces should have only method declarations. If you think you need member variables within this new type you're creating - you most likely want to make a class that you'll extend later on. Probably even an abstract class?

public abstract class Constants{
    String TEST = 'test';
}

I fail to understand how someone might think he later needs to write class MyConstants extends Constants (or implements if you'd follow the interface path)...

Please don't name your classes or variables Test, this can lead to some interesting compilation errors ;) And last but not least - this should probably be a public static final String testString...

share|improve this answer

You can't declare a constant in an interface, or for that matter implement any code other than method signatures (which you must then implement in a Class). An interface is a fairly generic term across programming languages. You can read about it in general here:

http://en.wikipedia.org/wiki/Interface_(computing)

Or, specifically in Apex here:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_interfaces.htm

However, it looks like you might want an enum (or a class) though for what you're doing, you can read up on that here:

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_enums.htm

Or if you simply want it as a class:

    public class Constants {
        public static final TEST = 'test';
}

So you can access it elsewhere as:

Constants.TEST;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.