In the doc from salesforce, there is the following example.
public class MyClass {
class RGB {
Integer red;
Integer green;
Integer blue;
RGB(Integer red, Integer green, Integer blue) {
this.red = red;
this.green = green;
this.blue = blue;
}
}
static Map<String, RGB> colorMap = new Map<String, RGB>();
static {
colorMap.put('red', new RGB(255, 0, 0));
colorMap.put('cyan', new RGB(0, 255, 255));
colorMap.put('magenta', new RGB(255, 0, 255));
}
}
Is that the recommended approach? What is the difference between that and the following one?
public class MyClass {
class RGB {
Integer red;
Integer green;
Integer blue;
RGB(Integer red, Integer green, Integer blue) {
this.red = red;
this.green = green;
this.blue = blue;
}
}
static Map<String, RGB> colorMap = new Map<String, RGB>{
'red' => new RGB(255, 0, 0),
'cyan' => new RGB(0, 255, 255),
'magenta' => new RGB(255, 0, 255)
};
}
I was under the impression that initialising the Map on the declaration is more efficient than creating an empty Map and putting elements one by one. Is that wrong?
I would understand that if it would be done on a getter, it would be a "lazy" initialisation, but is not the case, is it?
What am I missing here?