function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CodeBakerCodeBaker 

Unable to implement an interface with an anonymous class

Hey, sorry if this is a stupid question but I pretty lost with this issue.

 

I have an interface that I was hoping to implement with an anonymous class, but instead I get an unexpected token '{; error.

 

I am unable to find any documentation anywhere saying I am not allowed to do this and I'm able to do this within Java just fine.

 

Any tips if this is even possible would be helpful.

 

 

public with sharing class AnonymousEngine {
	private virtual interface Evaluator<T> {
		boolean expression(T val1, T val2);			
	}

	private class CreateEvaluator{
		private Evaluator<Double> eval;
		private Double number1, number2;
		public CreateEvaluator(Double a, Double b){
			number1 = a;
			number2 = b;
			
			eval = new Evaluator<Double>(){ // unexpected token: '{'
				public boolean expression(Double val1, Double val2){
					return (val1 == val2);
				}
			};
			
		}
		
		public boolean getResults(){
			eval.expression(number1, number2);
		}
		
	}	
}

 

Thanks in advance. 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Java allows anonymous classes because it is part of the Java language specification. Even though Apex Code runs on top of a Java VM-- the entire Force.com platform does, actually-- you must not confuse Apex Code with Java. Apex Code contains only a small subset of what Java is capable of, with the design of multi-tenant-friendly coding. You'll notice that you can not (yet) have threads for parallel execution, interprocess communication, asynchronous callbacks, event handlers, etc.

 

The "correct" method, for now, is to create named classes that implement the interface using the implements keyword. There is a feature request to allow anonymous classes. As you can see, you're not the first person to attempt this. I understand your frustration, as I have wished for this feature more than once, but for now, you'll have to write the extra code out manually.

All Answers

sfdcfoxsfdcfox

Java allows anonymous classes because it is part of the Java language specification. Even though Apex Code runs on top of a Java VM-- the entire Force.com platform does, actually-- you must not confuse Apex Code with Java. Apex Code contains only a small subset of what Java is capable of, with the design of multi-tenant-friendly coding. You'll notice that you can not (yet) have threads for parallel execution, interprocess communication, asynchronous callbacks, event handlers, etc.

 

The "correct" method, for now, is to create named classes that implement the interface using the implements keyword. There is a feature request to allow anonymous classes. As you can see, you're not the first person to attempt this. I understand your frustration, as I have wished for this feature more than once, but for now, you'll have to write the extra code out manually.

This was selected as the best answer
CodeBakerCodeBaker

I was afraid of this, but now I know. Thanks! Much appreciated!