t0mcat
t0mcat

Reputation: 5669

Initializing private variables inside a constructor

I have been looking at this code for a comparator. My question is there a specific benefit of initializing the two private variables inside the constructor(Done here in the default constructor). I know the second constructor can be used to create an instance with supplied values. What difference would it make if I do

private String sortBy = COLUMN_LAST_NAME;
private boolean ascending = true; 

I apologize if its a real simple and generic question.

public class CustomComparator implements Comparator<StudentList>
{   private String sortBy;
private boolean ascending;

public CustomComparator()
{
    sortBy = COLUMN_LAST_NAME;
    ascending = true;
}

public CustomComparator(String sortBy, boolean ascending)
{
    this.sortBy = sortBy;
    this.ascending = ascending;
}

Upvotes: 2

Views: 584

Answers (4)

Franco
Franco

Reputation: 7475

it's all about what you need, both constructor are good choices, obviuosly if you want to initialize the fields is the best choice but think if you use this class like an Entity on Hibernate or like a bean on Spring framework, if you dont write the empty constructor nothing will run fine...

if you think in DTO patterns, declaring inmutable fields like @Tomasz Nurkiewicz says then the parametrized constructor is the only choice....

but like i say before, it depends on requeriments...

Upvotes: 1

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340713

Actually it matters if you care about object immutability (and you should :-)). If both of your fields are final, you must initialize them like this:

public class CustomComparator implements Comparator<StudentList> {
    private final String sortBy;
    private final boolean ascending;

    public CustomComparator() {
        sortBy = COLUMN_LAST_NAME;
        ascending = true;
    }

    public CustomComparator(String sortBy, boolean ascending) {
        this.sortBy = sortBy;
        this.ascending = ascending;
    }
}

Or even better:

public class CustomComparator implements Comparator<StudentList> {
    private final String sortBy;
    private final boolean ascending;

    public CustomComparator() {
        this(COLUMN_LAST_NAME, true);
    }

    public CustomComparator(String sortBy, boolean ascending) {
        this.sortBy = sortBy;
        this.ascending = ascending;
    }
}

Upvotes: 3

pnt
pnt

Reputation: 1916

The best practice i've seen preached everywhere is have one constructor that takes all parameters, even if it means it has to be private, and then just call it from other constructors using this(..,..,...) while supplying appropriate values.

This will make you reuse as much code as possible and future fixes go to one place and one place only - no need to make double maintenance of code.

Your example would look like this:

public class CustomComparator implements Comparator<StudentList> {
    private String sortBy;
    private boolean ascending;

    public CustomComparator()
    {
        this(COLUMN_LAST_NAME, true);
    }

    public CustomComparator(String sortBy, boolean ascending)
    {
        this.sortBy = sortBy;
        this.ascending = ascending;
    }
}

Initialization is generally accepted to be in the constructor as to distinguish easier between static initialization of static members and per-instance init of instance variables. There is no performance difference.

Upvotes: 6

CarlosZ
CarlosZ

Reputation: 8669

I can't think of any difference and I prefer initializing in the variable declaration.

Upvotes: 0

Related Questions