DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Improving Repeated Analytics Workloads With Databricks Disk Cache
  • The LLM Selection War Story: Part 3 - Decision Framework Through Failure Tolerance
  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • Model Context Protocol Vs Agent2Agent: Practical Integration with Enterprise Data

Trending

  • How to Migrate From C/C++ to Rust: A Practical Guide for Embedded IoT Systems
  • Why AI Agents Need a Shared Enterprise Ontology
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • The SDLC After AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. A Use Case for Local Class Declaration

A Use Case for Local Class Declaration

Using local classes makes sense from an engineering point of view, but the principle of least surprise applies. Unless every team member is comfortable, don't use them.

By 
Nicolas Fränkel
·
Mar. 01, 17 · Opinion
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

One of the first things one learns when starting with Java development is how to declare a class into its own file. Potential later stages include:

  • Declaring multiple classes into the same file — with at most one of them public
  • Declaring static nested classes or inner classes
  • Declaring anonymous (inner) classes

But this doesn’t stop there: the JLS is a trove full of surprises. I recently learned classes can be declared inside any block, including methods. This is called local class declaration (§14.3).

A local class is a nested class (§8) that is not a member of any class and that has a name. All local classes are inner classes (§8.1.3). Every local class declaration statement is immediately contained by a block. Local class declaration statements may be intermixed freely with other kinds of statements in the block.

The scope of a local class immediately enclosed by a block (§14.2) is the rest of the immediately enclosing block, including its own class declaration. The scope of a local class immediately enclosed by in a switch block statement group (§14.11)is the rest of the immediately enclosing switch block statement group, including its own class declaration.

Cool, isn’t it? But using it just for the sake of it is not reason enough… until this week: I started to implement something like the Spring Boot Actuator in a non-Boot application, using Jackson to serialize the results.

Jackson offers several ways to customize the serialization process. For objects that require only to hide fields or change their names and which classes stand outside one’s reach, it offers mixins. As an example, let’s tweak serialization of the following class:

public class Person {
    private final String firstName;
    private final String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }
}


Suppose the requirement is to have givenName and familyName attributes. In a regular Spring application, the mixin class should be registered during the configuration of message converters:

public class WebConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        jackson2HttpMessageConverter.getObjectMapper().addMixIn(Person.class, PersonMixin.class);
        converters.add(jackson2HttpMessageConverter);
    }
}


Now, where does it make the most sense to declare this mixin class? The principle to declare something in the smallest possible scope applies: having it in a dedicated file is obviously wrong, but even a private nested class is overkill. Hence, the most restricted scope is the method itself:

public class WebConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        abstract class PersonMixin {
            @JsonProperty("givenName") abstract String getFirstName();
            @JsonProperty("familyName") abstract String getLastName();
        }
        jackson2HttpMessageConverter.getObjectMapper().addMixIn(Person.class, PersonMixin.class);
        converters.add(jackson2HttpMessageConverter);
    }
}


While this way makes sense from a pure software engineering point of view, there is a reason not to design code like this: the principle of least surprise. Unless every member of the team is aware and comfortable with local classes, this feature shouldn’t be used.

Use case Spring Framework

Published at DZone with permission of Nicolas Fränkel. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Improving Repeated Analytics Workloads With Databricks Disk Cache
  • The LLM Selection War Story: Part 3 - Decision Framework Through Failure Tolerance
  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • Model Context Protocol Vs Agent2Agent: Practical Integration with Enterprise Data

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook