Kick off your book project in 3 hours! Live workshop on Zoom. You’ll leave with a real book project, progress on your first chapter, and a clear plan to keep going. Saturday, June 6, 2026. Learn more…

Leanpub Header

Skip to main content

Elegant Java

Hands-on Guide to write Clean Code in Java

This book is 100% completeLast updated on 2026-06-05

To become a better software developer needs deep knowledge and practical skills in the field of software development and quality.

This book gives many examples which help you to write clean code. For more information like principlespatternstechniques, and tools have a look at my book Clean Code Fundamentals.

Minimum price

$14.99

$19.99

You pay

Author earns

$
PDF
EPUB
WEB
105
Pages
19,153Words
About

About

About the Book

Nothing affects the work of a team as much as bad, illegible, sloppy, and quickly written code that has not been designed carefully. Team dynamics can be improved, requirements can be redefined, and the schedule can be modified. However, if bad code takes over, it becomes more and more a burden for the team.

Programmers should develop all the time. Even if they think that they have sufficient skills and knowledge to move around in current projects, they should not stop there, and it is worthwhile for them to learn new concepts, approaches, language, and frameworks from time to time. Learning should be a journey and not a destination.

This book discusses the best practices of writing better code. It also contains many code examples in Java of increasing complexity.

This is a Forever Edition. That means that the book will see periodic updates, which are free for you. The book will use the latest Java version for the examples provided.

Share this book

Bundle

Bundles that include this book

Author

About the Author

Martin Hock

Martin is a principal software engineer with over 20 years of experience in application design and development. His primary focus is on lightweight Java and open source technologies with expertise in software architecture, web technologies, and the Spring ecosystem.

He has found that teaching is the best way to learn software development. He is a part-time lecturer at two universities and teaches courses on Java, Software Craftsmanship and Clean Code.

Contents

Table of Contents

Preface

  1. What about the code examples and typographic conventions?
  2. Which literature is this book based on?
  3. Giving feedback?

1Java Best Practices

  1. 1.1Avoid instantiation for utility classes
  2. 1.2Use immutable objects
  3. 1.3Prefer records for immutability
  4. 1.4Consider static factory methods for expressive construction
  5. 1.5Provide immutable decorators for sensitive mutable classes
  6. 1.6Use pattern matching for instanceof
  7. 1.7Consider using sealed classes and interfaces
  8. 1.8Use record patterns for destructuring
  9. 1.9Avoid constant interfaces
  10. 1.10Avoid global constant classes
  11. 1.11Favour composition over inheritance
  12. 1.12Consider using lambdas
  13. 1.13Avoiding Primitive Obsession
  14. 1.14Use the @Override Annotation
  15. 1.15Use the @FunctionalInterface Annotation
  16. 1.16Prefer returning Null-Objects over null
  17. 1.17Avoid null as a method parameter
  18. 1.18Prefer enhanced loops over for loops
  19. 1.19Code against interfaces not implementations
  20. 1.20Use existing exceptions
  21. 1.21Use try-with-resources for closeable resources
  22. 1.22Validate method parameter
  23. 1.23Minimize method parameters for improved readability
  24. 1.24Prevent NullPointerException for String comparison
  25. 1.25Safely cast long to int
  26. 1.26Convert integers to floating-point for floating-point math operations
  27. 1.27Prefer BigDecimal for money and exact decimal calculations
  28. 1.28Use generics in favor of raw types
  29. 1.29Prefer enums over int constants
  30. 1.30Prefer the == operator over equals for comparing enum values
  31. 1.31Be aware of the contract between equals and hashCode
  32. 1.32Use text blocks for multi-line strings
  33. 1.33Use always braces for the body of all statements
  34. 1.34Simplifying boolean expressions
  35. 1.35Pre-calculate the length in loops
  36. 1.36Avoid constructor instantiation of String
  37. 1.37Use StringBuilder for concatenation
  38. 1.38Reduce lookups in collection containers
  39. 1.39Instantiate wrapper objects with valueOf
  40. 1.40Use entrySet for iterating
  41. 1.41Use isEmpty() for String length
  42. 1.42Use switch expressions for concise control flow
  43. 1.43Avoid using the default case in switch statements for sealed classes
  44. 1.44Prefer method references over lambdas for conciseness
  45. 1.45Use the orElse and orElseGet methods for optional values
  46. 1.46Use Optional as a return type, not as a general-purpose value
  47. 1.47Enhance code clarity with EnumMap
  48. 1.48Prefer java.time types and inject Clock
  49. 1.49Use local variable type inference with var judiciously
  50. 1.50Practice defensive programming
  51. 1.51Prefer higher-level concurrency utilities over raw threads
  52. 1.52Implement effective logging practices
  53. 1.53Write tests as executable documentation

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub