Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any problem with doing the main work of a class in its constructor?

I have always felt that in general the main work of a class should be done in its instance methods, while the constructor should only get the instance into a usable inital state.

But I find that in practice there are situations where it seems to make more sense to put essentially all the actual work into the constructor.

One example: I need to retrieve some DBMS-specific information from the database. The most natural way to me seemed to have a class DBMSSpecInfo, with a constructor:

public DBMSSpecInfo(java.sql.Connection conn) throws SQLException{
  // ... retrieve info from DBMS
}

/** @returns max size of table in kiB */
public int getMaxTableSize() {//...}

/** @returns max size of index in kiB */
public int getMaxIndexSize() {//...}

/** @returns name of default schema */
public String getDefaultSchema() {//...}

You would construct the class once, the constructor would fetch all data, then you could use various getters to retrieve the info you need.

Of course I could put the method somewhere else, and only use DBMSSpecInfo for the return value (essentially using DBMSSpecInfo only as a value holder), but it feels ugly to create a class just for returning values from a single function.

So what do you think? Are there problems with performing the main work in the constructor? Is it "un-idiomatic" in Java? Or is it an acceptable (though possibly uncommon) practice?

like image 607
sleske Avatar asked Sep 29 '10 10:09

sleske


People also ask

What should you do in a constructor?

The purpose of constructor is to initialize the object of a class while the purpose of a method is to perform a task by executing java code. Constructors cannot be abstract, final, static and synchronised while methods can be. Constructors do not have return types while methods do.

Which constructor Cannot be applied?

Constructor X in Class Y Cannot be Applied to Given Types Just as with ordinary methods, constructors need to be invoked with the correct number, type, and order of arguments. Fig. 2.1 shows how calling a constructor by omitting an argument triggers the constructor X in class Y cannot be applied to given types error.

Which of the following are true about constructors in Java?

What is true about constructor? Explanation: Constructor returns a new object with variables defined as in the class. Instance variables are newly created and only one copy of static variables are created. 6.

Does a class need a constructor?

You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass.


1 Answers

The main practical problem is unit-testing - you won't be able to instantiate the object without doing actual work. (Or you'd have to mock all the classes that participate in this work).

Related talk: OO Design for testability. It gives examples of why doing work in constructors is bad for unit-testing.

like image 95
Bozho Avatar answered Sep 27 '22 23:09

Bozho