Skip to content

Move statements before super(..) in Java constructor #733

@JohannisK

Description

@JohannisK

What problem are you trying to solve?

Jep 513 calls for allowing statements before the call to super() in Java constructors, allowing for simple assignments and validations before doing expensive construction.

What precondition(s) should be checked before applying this recipe?

Java version >= 25
(Also double check Jep 513 lands in Java 25)

Describe the situation before applying the recipe

class A {
    public A(String bar) {
        super();
        if(bar.equals("test"))
            throw new RuntimeException();
    }
}

Describe the situation after applying the recipe

class A {
    public A(String bar) {
        if(bar.equals("test"))
            throw new RuntimeException();
        super();
    }
}

Any additional context

https://openjdk.org/jeps/513 has several examples and considerations on what statements should be supported before calls to super(..)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions