> For the complete documentation index, see [llms.txt](https://pawelwlodarski.gitbook.io/java/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pawelwlodarski.gitbook.io/java/java-workshops/jeps/jdk24-previews/jep-492-flexible-constructor-bodies-third-preview.md).

# JEP 492: Flexible Constructor Bodies (Third Preview)

### 🚀 What is it?

Traditionally, Java constructors have required **explicit placement** of `super()` or `this()` **as the first line** in the constructor.\
**JEP 492** **relaxes** this rule:\
➡️ Now, you can run code **before** calling `super()` or `this()` — as long as you **don’t access `this` or instance members** before initialization.

## Fixed calling subclass overriden method

Although the Java language [allows constructors to invoke overridable methods](https://docs.oracle.com/javase/specs/jls/se23/html/jls-12.html#jls-12.5-300), it is considered bad practice: Item 19 of [Effective Java (Third Edition)](https://www.oreilly.com/library/view/effective-java-3rd/9780134686097/) advises that "Constructors must not invoke overridable methods." To see why it is considered bad practice, consider the following class hierarchy:

```
class Super {

    Super() { overriddenMethod(); }

    void overriddenMethod() { System.out.println("hello"); }

}

class Sub extends Super {

    final int x;

    Sub(int x) {
        this.x = x;    // Initialize the field
        super();       // Then invoke the Super constructor explicitly
    }

    @Override
    void overriddenMethod() { System.out.println(x); }

}
```

## RUN:

```
FlexibleConstructorDemo
```

There are two cases : &#x20;

* simple validation before calling super
* list of illegal this references during class creation
  * special cases with inner classes
