5

I want to use Constructor Overloading in my class and I also would like to have some final variables to define.

The structure I would like to have is this:

public class MyClass{
    private final int variable;

    public MyClass(){
        /* some code and 
           other final variable declaration */
        variable = 0;
    }

    public MyClass(int value){
        this();
        variable = value;
    }
}

I would like to call this() to avoid to rewrite the code in my first constructor but I have already defined the final variable so this give a compilation error. The most convenient solution I have in mind is to avoid the final keyword but of course it is the worst solution.

What can be the best way to define the variable in multiple constructors and avoid code repetitions?

3 Answers 3

8

You are almost there. Rewrite your constructors such way that your default constructor call the overloaded constructor with value 0.

public class MyClass {
    private final int variable;

    public MyClass() {
        this(0);
    }

    public MyClass(int value) {
        variable = value;
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

I came along a similar situation and reading this helped me solving the problem of not being able to Reference “X” Before Supertype Constructor Has Been Called baeldung.com/…
0

If you have small number variable then it is ok to use Telescoping Constructor pattern.
MyClass() { ... } MyClass(int value1) { ... }
Pizza(int value1, int value2,int value3) { ... }

                                                                                                If there is multiple variable and instead of using method overloading you can use builder pattern so you can make all variable final and will build object gradually.


public class Employee {
    private final int id;
    private final String name;

    private Employee(String name) {
        super();
        this.id = generateId();
        this.name = name;

    }

    private int generateId() {
        // Generate an id with some mechanism
        int id = 0;
        return id;
    }

    static public class Builder {
        private int id;
        private String name;

        public Builder() {
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Employee build() {
            Employee emp = new Employee(name);
            return emp;
        }
    }
}

Comments

0

You can not assign final variable in both constructors. If you want to keep the final variable and also want to set via constructor then one possibility that you will dedicate one constructor to set the final variable and also include common code functionality needed by the class. Then call this from another constructor like this(*finalVariableValue*);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.