B
B
Boris Khripko2017-03-30 17:52:23
Java
Boris Khripko, 2017-03-30 17:52:23

How to call one constructor from another with a final variable?

Hello.
I have a structure like this:

public class A{
    private final String someString;
    private Object[] array;

    public A(int size){
        if(size<0){
            throw new Exception();
        }
        array=new Object[size];
        someString="defaultString"
    }

    public A(int size, String someString){
        if(size<0){
            throw new Exception();
        }
        array=new Object[size];
        this.someString = someString;
    }
}

But I would like to know how to optimize this case. So that in one constructor you can call another. It's just that if you call a constructor with one in a constructor with two arguments, then it will no longer allow you to change the variable. Pliz help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-03-30
@BariSovich

Well, yes, because it is necessary not to call a constructor with one parameter from a constructor with two parameters, but vice versa. And pass "defaultString" there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question