What's the name of this construction?

I'm reading a code that I didn't write. I stumbled upon the following statement:

    context.checking(new org.jmock.Expectations() {
        {
            allowing(habilitationManager).hasRole(RoleDtoEnum.ROLE_NEWS);
            will(returnValue(true));
            allowing(habilitationManager).hasRole(RoleDtoEnum.ROLE_STAT);
            will(returnValue(true));
            allowing(habilitationManager).getUser();
            will(returnValue(getUserMock()));
            oneOf(parametreService).getParametre(PPP);
            will(returnValue(getMockPPP()));
        }
    });

I understand that the methods called inside the second { ... } are Expectations methods.

  • But how do you call this kind of code writing ?
  • Especially how do you call the second { ... } ?
Jon Skeet
people
quotationmark

It's an anonymous class, with an instance initializer block in it. So to separate the two out:

// This is an anonymous class
Expectations expectations = new Expectations() {
    // Class body here
};

class Foo {

    // This is an instance initializer block in a normal class
    {
        System.out.println("You'll see this via either constructor");
    }

    Foo(int x) {}

    Foo(String y) {}
}

An instance initializer is implicitly called at the same time as instance variable initializers, in textual order, before the body of any constructor.

people

See more on this question at Stackoverflow