Find your content:

Search form

You are here

Getting error while declaring a constant in Interface

 
Share
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?


Attribution to: Pramod Kumar

Possible Suggestion/Solution #1

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;

Attribution to: Phil Hawthorn

Possible Suggestion/Solution #2

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...


Attribution to: eyescream
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/5199

My Block Status

My Block Content