6

Is it possible to rewrite the following a bit more concise that I don't have to repeat myself with writing this.x = x; two times?

public class cls{
    public int x = 0;
    public int y = 0;
    public int z = 0;

    public cls(int x, int y){
        this.x = x;
        this.y = y;
    }

    public cls(int x, int y, int z){
        this.x = x;
        this.y = y;
        this.z = z;
    }
}

5 Answers 5

15

BoltClock's answer is the normal way. But some people (myself) prefer the reverse "constructor chaining" way: concentrate the code in the most specific constructor (the same applies to normal methods) and make the other call that one, with default argument values:

public class Cls {
    private int x;
    private int y;
    private int z;

    public Cls(int x, int y){
         this(x,y,0);
    }

    public Cls(int x, int y, int z){
        this.x = x;
        this.y = y;
        this.z = z;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I usually make this last constructor private or protected, so I can control the ways to get there.
10

Call the other constructor within this overloaded constructor using the this keyword:

public cls(int x, int y, int z){
    this(x, y);
    this.z = z;
}

1 Comment

And it is called "constructor chaining "
1

Read about constructor overloading http://www.javabeginner.com/learn-java/java-constructors

Comments

0

You can use initilization block for this purpose.

5 Comments

@Nikita Rybak This isn't code golf - the idea isn't to make the code as short as possible, just to stop duplication which can lead to overlooking changes to the code in the future - ie changing one constructor but not the other.
@Noel I just don't understand what's proposed to do, that's all.
@Zacky I'd advise against dynamic initializers, they make code less obvious and readable to others.
@Noel M I think Nikita's "11 more characters" was to get his post up to the minimum length, not a comment about the length of the code.
Ah, sorry Nikita, my misunderstanding
0

Very simple: Just write an initialize function like this:

public class cls{
    public int x = 0;
    public int y = 0;
    public int z = 0;

    public cls(int x, int y){
       init(x,y,0);
    }

    public cls(int x, int y, int z){
       init(x,y,z);
    }

     public void init(int x, int y, int z ) {
        this.x = x;
        this.y = y;
        this.z = z;  
    }
}

3 Comments

the trouble with this is that you can't make the variables final and you also can't make sure that init can be called from constructors only.
It would be better to make the init method private. But the selected answer does the same, with less code.
@seanizer Your right - but the OP dident tell us if he need the code elsewhere, just that he don't want to repeat. So i dident bother about such things, and dident use constructor chaining. @Alexandre Your right too, see my comment for seanizer.

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.