java - Final variables "might not have been initialized" in a multiple-constructor class - Stack Overflow

admin2025-04-16  7

I have a class with two different final variables and two different constructors.

public class Parent {
    private final String name;
    private final String dob;

    public Parent() {
        System.out.println("no args constructor");
    }

    public Parent(String name, String dob) {
        this.name = name;
        this.dob = dob;
        System.out.println("in parent constructor");
    }
}

Trying to compile or run it throws an error like this:

Parent.java:7: error: variable name might not have been initialized
    }
    ^
1 error
error: compilation failed

Why is my program throwing this error, and how can I fix it?

I have a class with two different final variables and two different constructors.

public class Parent {
    private final String name;
    private final String dob;

    public Parent() {
        System.out.println("no args constructor");
    }

    public Parent(String name, String dob) {
        this.name = name;
        this.dob = dob;
        System.out.println("in parent constructor");
    }
}

Trying to compile or run it throws an error like this:

Parent.java:7: error: variable name might not have been initialized
    }
    ^
1 error
error: compilation failed

Why is my program throwing this error, and how can I fix it?

Share Improve this question edited Feb 4 at 5:50 Anerdw 2,1023 gold badges16 silver badges40 bronze badges asked Feb 4 at 0:29 rudrarajrudraraj 335 bronze badges 7
  • 5 They're not initialized from your no-args constructor. The compiler doesn't allow any path that leaves the fields uninitialized. – shmosel Commented Feb 4 at 0:31
  • 2 This question is similar to: Why must a final variable be initialized before constructor completes?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Anerdw Commented Feb 4 at 0:43
  • 4 @Basil Why not? Is OP's problem not that their no-argument constructor does not initialize the final variables? – Anerdw Commented Feb 4 at 0:49
  • 1 @Anerdw The other Question does not address multiple constructors. You’re making cognitive leaps that cannot be made by the newbies reading this Question. – Basil Bourque Commented Feb 4 at 1:00
  • 2 @user207421 I see that as a reason to edit, not downvote. – Anerdw Commented Feb 4 at 12:43
 |  Show 2 more comments

1 Answer 1

Reset to default 4

Populate finalfields by the end of construction

You have two constructors. One populates the final fields, the other does not. The compiler alerts you to the fact that you neglected to populate the final fields in one of the constructors.

Being final means a value cannot be assigned later. You must populate final fields by the end of construction.

Solutions:

  • Delete the no-arg constructor if not needed.
  • Edit the no-arg constructor to assign some kind of default values to the final fields.
转载请注明原文地址:http://www.anycun.com/QandA/1744743997a86995.html