Efficiency of Java “Double Brace Initialization”?

Here’s the problem when I get too carried away with anonymous inner classes: 2009/05/27 16:35 1,602 DemoApp2$1.class 2009/05/27 16:35 1,976 DemoApp2$10.class 2009/05/27 16:35 1,919 DemoApp2$11.class 2009/05/27 16:35 2,404 DemoApp2$12.class 2009/05/27 16:35 1,197 DemoApp2$13.class /* snip */ 2009/05/27 16:35 1,953 DemoApp2$30.class 2009/05/27 16:35 1,910 DemoApp2$31.class 2009/05/27 16:35 2,007 DemoApp2$32.class 2009/05/27 16:35 926 DemoApp2$33$1$1.class 2009/05/27 16:35 4,104 … Read more

Error “initializer element is not constant” when trying to initialize variable with const

In C language, objects with static storage duration have to be initialized with constant expressions, or with aggregate initializers containing constant expressions. A “large” object is never a constant expression in C, even if the object is declared as const. Moreover, in C language, the term “constant” refers to literal constants (like 1, ‘a’, 0xFF … Read more

Using special auto start servlet to initialize on startup and share application data

None of both is the better approach. Servlets are intended to listen on HTTP events (HTTP requests), not on deployment events (startup/shutdown). CDI/EJB unavailable? Use ServletContextListener @WebListener public class Config implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { // Do stuff during webapp’s startup. } public void contextDestroyed(ServletContextEvent event) { // Do stuff during webapp’s … Read more

What is Double Brace initialization in Java?

Double brace initialisation creates an anonymous class derived from the specified class (the outer braces), and provides an initialiser block within that class (the inner braces). e.g. new ArrayList<Integer>() {{ add(1); add(2); }}; Note that an effect of using this double brace initialisation is that you’re creating anonymous inner classes. The created class has an … Read more

Do the parentheses after the type name make a difference with new?

Let’s get pedantic, because there are differences that can actually affect your code’s behavior. Much of the following is taken from comments made to an “Old New Thing” article. Sometimes the memory returned by the new operator will be initialized, and sometimes it won’t depending on whether the type you’re newing up is a POD … Read more

Is there a difference between copy initialization and direct initialization?

C++17 Update In C++17, the meaning of A_factory_func() changed from creating a temporary object (C++<=14) to just specifying the initialization of whatever object this expression is initialized to (loosely speaking) in C++17. These objects (called “result objects”) are the variables created by a declaration (like a1), artificial objects created when the initialization ends up being … Read more