views:

130

answers:

5

Is there any sense to declare default constructor in Java?

class MyClass {
  public MyClass(){}
  public MyClass( T someArgs){
    //somecode
  }

}
+8  A: 

If you have a non default constructor, then default constructor is not implicitly available. It is a good idea not to put one if it is your goal is not to have anyone do :

MyClass myClass = new MyClass();
fastcodejava
+5  A: 

Java adds a default public no-args constructor if you specify no other constructors so there is a point in putting one if you need one and specify another. So in your case if you defined your class like this:

class MyClass {
  public MyClass( T someArgs){
    //somecode
  }
}

then you couldn't do this:

MyClass a = new MyClass();

but if it were defined like this:

class MyClass {
  // no constructors
}

you could.

Often no-arg constructors are used to specify pseudo-default arguments. For example:

class Switch {
  private boolean on;

  public Switch() { this(true); }
  public Switch(boolean on) { this.on = on; }
  public boolean isOn() { return on; }
  public boolean toggle() { on = !on; }
  public boolean set(boolean on) { this.on = on; return this.on; }
}
cletus
+4  A: 

Yes, when you have other constructors.

  • It is required when you want to create object without passing any parameters to constructor.

  • In most cases it is required for reflection, especially when you work with certain reflection-based frameworks and libraries (e.g. serialization, Hibernate, Hessian).

  • Using setters instead generally may give you better control. It also works well with inheritance (where you'd need to explicitly call the constructor for all specialized classes, as constructors are not virtual.

Konrad Garus
A: 

Certain frameworks will require that you have a zero-argument/default constructor. For example, you need one to run a class as a JUnit test case. [edit: removed incorrect statement]

This is due to their use of reflection.

amac
No. Serializable objects do not need default constructors.
EJP
You do in some cases (but yes, not all): http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html
amac
Read it yourself. Serializable classes do not need default constructors. Period. It is the first non-serializable superclass that needs a default constructor.
EJP
+2  A: 

Besides all previous arguments for the no-args constructor, it is a mandatory element of JavaBeans, as these beans can be created using reflection (specifically by calling Class.newInstance). As a consequence, any framework relying upon JavaBeans will make this no-args constructor a mandatory part of your architecture.

There is also an added benefit, as a no-args constructor can help introduce a kind of fluent interface, by letting you chain setter calls. As an example, in a company I worked for, Is used to define beside setters and getter with methods like this :

class MyClass {
    privaite int index;
    public int getIndex() { return index; }
    public void setindex(int index) { this.index = index; }

    public MyClass withIndex(int index) {
        setIndex(index);
        return this;
    }
}

Allowing me to create objects the following way :

MyClass myObject = new MyClass().withIndex(2);

This was a very useful way of creating objects without to define bug constructors.

Riduidel