Creando y Destruyendo Objetos en Java y Kotlin

Guía de Effective Java aplicada a Kotlin: patrones de creación y destrucción de objetos, fábrica estática, builder, singleton, inyección de dependencias y ejemplos en Java y Kotlin para código más robusto y mantenible.

11 sept 2025 • 5 min read • Q2BSTUDIO Team

Inteligencia-Artificial-

Introducción a la serie Effective Java de Joshua Bloch y su influencia en la escritura de código Java robusto, eficiente y mantenible. Recientemente he explorado Kotlin y quiero aplicar los principios y buenas prácticas del libro a Kotlin. En esta entrega resumo el Capítulo 2 Creando y Destruyendo Objetos y sus items esenciales, aportando implementaciones en Java y Kotlin para afianzar conceptos en ambos lenguajes.

Capítulo 2: Creando y Destruyendo Objetos

Item 1 Considerar métodos de fábrica estáticos en lugar de constructores Resumen Los métodos de fábrica estáticos son una alternativa a los constructores que aportan nombres significativos, flexibilidad y la posibilidad de devolver instancias de subclases.

Java public class Color { private final int value; private Color(int value) { this.value = value; } public static Color ofValue(int value) { return new Color(value); } public static Color ofRGB(int r, int g, int b) { return new Color((r * 65536) + (g * 256) + b); } }

Kotlin class Color private constructor(val value: Int) { companion object { fun ofValue(value: Int) = Color(value) fun ofRGB(r: Int, g: Int, b: Int) = Color((r shl 16) or (g shl 8) or b) } }

En Kotlin el companion object permite definir funciones asociadas a la clase, ofreciendo la misma funcionalidad que métodos estáticos de Java con sintaxis más concisa.

Item 2 Considerar un builder cuando hay muchos parámetros Resumen Cuando una clase tiene muchos parámetros de constructor, el patrón builder mejora la legibilidad, permite omitir parámetros opcionales y facilita crear objetos inmutables.

Java public class NutritionFacts { private final int servingSize; private final int servings; private final int calories; private final int fat; private final int sodium; private final int carbohydrate; public static class Builder { private final int servingSize; private final int servings; private int calories = 0; private int fat = 0; private int sodium = 0; private int carbohydrate = 0; public Builder(int servingSize, int servings) { this.servingSize = servingSize; this.servings = servings; } public Builder calories(int val) { calories = val; return this; } public Builder fat(int val) { fat = val; return this; } public Builder sodium(int val) { sodium = val; return this; } public Builder carbohydrate(int val) { carbohydrate = val; return this; } public NutritionFacts build() { return new NutritionFacts(this); } } private NutritionFacts(Builder builder) { servingSize = builder.servingSize; servings = builder.servings; calories = builder.calories; fat = builder.fat; sodium = builder.sodium; carbohydrate = builder.carbohydrate; } }

Kotlin class NutritionFacts(val servingSize: Int, val servings: Int, val calories: Int = 0, val fat: Int = 0, val sodium: Int = 0, val carbohydrate: Int = 0)

En Kotlin los valores por defecto de parámetros reducen la necesidad de un builder para muchos casos, mejorando la legibilidad al instanciar objetos.

Item 3 Garantizar la propiedad singleton con constructor privado o enum Resumen Un singleton debe tener una sola instancia por JVM. En Java se puede usar un campo estático final con constructor privado o usar un enum para mayor seguridad frente a serialización. En Kotlin la declaración object es la forma idiomática.

Java private constructor public class DatabaseConnection { public static final DatabaseConnection INSTANCE = new DatabaseConnection(); private DatabaseConnection() {} public void connect() { /* conectar a base de datos */ } }

Java enum public enum DatabaseConnectionEnum { INSTANCE; public void connect() { /* conectar a base de datos */ } }

Kotlin object DatabaseConnection { fun connect() { /* conectar a base de datos */ } }

Item 4 Impedir la instanciación con un constructor privado Resumen Para clases utilitarias que solo ofrecen métodos estáticos, usar un constructor privado evita instanciación accidental y deja claro el propósito de la clase.

Java public class UtilityClass { private UtilityClass() { throw new AssertionError(); } public static void utilityMethod() { /* método utilitario */ } }

Kotlin class UtilityClass private constructor() { companion object { fun utilityMethod() { /* método utilitario */ } } } // o alternativamente object UtilityClass { fun utilityMethod() { /* método utilitario */ } }

Item 5 Preferir inyección de dependencias en lugar de acoplar recursos Resumen La inyección de dependencias desacopla componentes, facilita pruebas y mantiene flexibilidad para cambiar implementaciones concretas.

Java acoplamiento public class LexiconBad { private static final Dictionary dictionary = new MerriamWebster(); public LexiconBad() {} public void lookup(String word) { dictionary.define(word); } }

Java inyección public class Lexicon { private final Dictionary dictionary; public Lexicon(Dictionary dictionary) { this.dictionary = java.util.Objects.requireNonNull(dictionary); } public void lookup(String word) { dictionary.define(word); } }

Kotlin class Lexicon(private val dictionary: Dictionary) { fun lookup(word: String) { dictionary.define(word) } } interface Dictionary { fun define(word: String) } class MerriamWebster : Dictionary { override fun define(word: String) { /* definir palabra */ } }

Beneficios de la inyección incluyen pruebas más sencillas con mocks, menor acoplamiento y mayor capacidad de mantenimiento.

Sobre Q2BSTUDIO Somos Q2BSTUDIO, empresa de desarrollo de software y aplicaciones a medida especializada en soluciones a medida para empresas. Ofrecemos servicios de software a medida, aplicaciones a medida, arquitecturas cloud y consultoría en inteligencia artificial y ciberseguridad. Nuestra experiencia incluye implementación de servicios cloud aws y azure, proyectos de servicios inteligencia de negocio y despliegue de soluciones power bi para informes y dashboards. Integramos ia para empresas y agentes IA adaptados a procesos de negocio para automatizar tareas y mejorar decisiones.

Si necesita una solución de desarrollo de aplicaciones y software a medida o desea explorar servicios de inteligencia artificial para su organización, en Q2BSTUDIO diseñamos e implementamos desde agentes IA hasta pipelines en la nube, con foco en ciberseguridad y cumplimiento. Palabras clave que aplicamos en nuestros contenidos y servicios incluyen aplicaciones a medida, software a medida, inteligencia artificial, ciberseguridad, servicios cloud aws y azure, servicios inteligencia de negocio, ia para empresas, agentes IA y power bi.

Conclusión Aplicar los consejos de Effective Java en Java y Kotlin mejora la calidad del código y la arquitectura de aplicaciones. Elegir fábricas estáticas, builders cuando corresponde, singletons bien implementados, clases no instanciables y preferir la inyección de dependencias son prácticas que reducen errores y aumentan mantenibilidad. En Q2BSTUDIO acompañamos a las empresas en la adopción de estas prácticas dentro de proyectos de software a medida, migraciones cloud y soluciones de inteligencia artificial.

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Artificial intelligence

AI agents, chatbots, and intelligent assistants that automate tasks and serve your customers 24/7 to improve the efficiency of your business.

More info

Software Development

Web, mobile, and desktop applications, intranets, e-commerce, SaaS, and management platforms designed for your company's specific needs.

More info

Cloud services

Migration, infrastructure, managed hosting, high availability, and security on Microsoft Azure and Amazon Web Services to help your business scale without limits.

More info

Cybersecurity and pentesting

Security audits, penetration testing and protection of applications, data and infrastructure on-premise and cloud, with ethical hacking and regulatory compliance.

More info

Business Intelligence

Dashboards and data analysis with Power BI: we integrate your sources, design dashboards and KPIs and turn your data into decisions.

More info

Process automation

We automate repetitive tasks and connect your applications with n8n, Power Automate, Make, and RPA, eliminating manual work and increasing productivity.

More info

Training for Companies

We train your teams in technology with criteria: web development, databases, Git, best practices and security, automation with n8n, artificial intelligence for companies and creation of AI solutions with Azure AI Foundry.

More info

Code Auditing

We audit the code that you, your team or an AI create: we tell you what is good and what to improve, we secure it and make it ready for production, web or app.

More info

AI Image Generation

We create for you the images that your business needs with artificial intelligence: product, networks, advertising, illustration and avatars. You tell us what you want and we deliver it ready to use.

More info

AI Video Generation

We create videos with artificial intelligence for you: promotional, networking, virtual presenters, dubbing and animations. You tell us the idea and we will deliver it assembled and ready to publish.

More info

AI Conversational Avatars

We create conversational avatars with AI – digital humans with a face and voice – that serve your customers and teams with the knowledge of your company, on your website, interactive monitors, WhatsApp or Teams.

More info

Online Marketing and AI

Google Ads, Meta Ads, LinkedIn Ads and AI Engine Positioning (GEO/AEO): we attract customers and make your brand appear where they search for you, also on ChatGPT, Gemini and Perplexity.

More info

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.

Live Chat