Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why might one also use a blank constructor?

I was reading some Java recently and came across something (an idiom?) new to me: in the program, classes with multiple constructors would also always include a blank constructor. For example:

public class Genotype {
  private boolean bits[];
  private int rating;
  private int length;
  private Random random;

  public Genotype() {              //  <= THIS is the bandit, this one right here
    random = new Random();
  }

  /* creates a Random genetoype */
  public Genotype(int length, Random r) {
    random = r;
    this.length = length;
    bits = new boolean[length];

    for(int i=0;i<length;i++) {
        bits[i] =random.nextBoolean();
    }
  }

  /* copy constructor */
  public Genotype(Genotype g,Random r) {
    random = r;
    bits = new boolean[g.length];
    rating = g.rating;
    length = g.length;

    for(int i=0;i<length;i++) {
        bits[i] = g.bits[i];
    }

  }
}

The first constructor doesn't seem to be a "real" constructor, it seems as though in every case one of the other constructors will be used. So why is that constructor defined at all?

like image 890
Ziggy Avatar asked Dec 31 '08 08:12

Ziggy


People also ask

What is the use of blank constructor?

Empty constructor build GENERIC object, and constructor with parameters build objects with more specific information. Let's say the example above, if I create an instance object using the empty constructor: Person p1 = new Person(); -- it will still create an object but without any properties in it?

Why do we need empty constructor in spring boot?

Why do we need a empty constructor for the fragment? Your Fragment subclasses need a public empty constructor as this is what's being called by the framework. @TNR as all Fragment abstractions have an empty constructor super() would be fruitless, as the parent class has broken the empty public constructor rule.

What is an empty constructor called?

A default constructor is a constructor created by the compiler if we do not define any constructor(s) for a class. Here is an example: public class Student { String firstName; String lastName; int age; public static void main(String args[]) { Student myStudent = new Student(); myStudent.

Why do we need empty constructor in C++?

Answer: C++ Empty constructor necessity depends upon class design requirements. We know that C++ class constructor is called when we create an object of a class. If a class is not required to initialize its data member or does not contain data member, there is no need to write empty constructor explicitly.


1 Answers

I am not sure that the code you were reading was high quality (I've reviewed some bioinformatics code in the past and it is unfortunately often not written by professional developers). For example, that third constructor is not a copy constructor and generally there are problems in this code, so I wouldn't "read too much into it".

The first constructor is a default constructor. It only initializes the bare minimum and lets users set the rest with getters and setters. Other constructors are often "convenience constructors" that help create objects with less calls. However, this can often lead to inconsistencies between constructors. In fact, there is recent research that shows that a default constructor with subsequent calls to setters is preferable.

There are also certain cases where a default constructor is critical. For example, certain frameworks like digester (used to create objects directly from XML) use default constructors. JavaBeans in general use default constructors, etc.

Also, some classes inherit from other classes. you may see a default constructor when the initialization of the parent object is "good enough".

In this specific case, if that constructor was not defined, one would have to know all the details in advance. That is not always preferable.

And finally, some IDEs automatically generate a default constructor, it is possible that whoever wrote the class was afraid to eliminate it.

like image 177
Uri Avatar answered Sep 18 '22 12:09

Uri