Why would an academic write a textbook for free? Recently, I became aware of a Java programming textbook
Introduction to Programming Using Java, Seventh Edition
which is to my surprise, free as in:


  The most recent version of this book is always available, at no charge, for downloading and for on-line use.


Question: Why would the author (David J. Eck) make this textbook free?

I have found that almost always,
authors of academic textbooks prefer to make money
from sales of their textbooks,
rather than writing a textbook which is available freely without cost.
(I don't have any statistics to back up this assertion.
However, throughout my undergrad and my PhD studies,
in about 95% of the courses which I took,
the prescribed textbook was published by an academic publisher such as
McGraw-Hill, Pearson or Springer.)

Closely related question:
Massimo Ortolano
claims in this comment that most authors would not be able to make much money
from sales of academic textbooks they have written.
Can someone give me an estimated amount of earnings per textbook sold?
As the author of this particular free textbook, I can say that the previous answers have covered things pretty well.  "Introduction to programming using Java" started in 1996 as a set of class notes written in HTML with embedded Java applets -- which all seemed pretty exciting in the heady early days of the Internet.  I had published an earlier textbook in the usual way, and made not much money from it, which is true of the large majority of published textbooks.  I had recently been promoted to full professor, possibly the most secure job in the world where the only further promotion available to me would move me into (shudder) administration, so it would make little difference to my career if I self-published a book instead of submitting it to a regular publisher.

I lost creative control over my regularly published textbook.  The copyright was assigned to the publisher until it goes out of print, which it will probably never do in these days of print-on-demand.  There has never been a second edition.  My Java textbook, on the other hand, has gone through seven versions and has been updated fairly regularly.  I get the ego boost of hearing from people all over the world who have read it (something I can never seem to get most of my own students to do).  The web site still gets hundreds of visits every day.  It has certainly had more impact on the world than if I were selling it.  And if someone doesn't like it, I can politely point out that they can write their own textbook, or even make a modified version of mine.

So, my main motivation was to write a textbook that I could use in my own courses.  Selling it would make me very little profit.  And since I use free software all the time, it's also a way that I could give back something to the open source / creative commons community.