Struct like objects in Java
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In programming, a struct (short for "structure") is a composite data type that groups together variables under a single name, enabling efficient data management and manipulation. While Java does not natively support structs as in languages like C or C++, it offers alternative constructs to achieve similar functionality. This article delves into the concept of struct-like objects in Java, exploring technical aspects, examples, and best practices.
Struct-Like Objects in Java
Java is an object-oriented language that encourages encapsulation and abstraction, commonly utilizing classes and interfaces. Although Java lacks an explicit struct keyword, developers can implement struct-like behavior through classes and records (introduced in Java 14 as a preview feature and standard in Java 16).
Key Characteristics
Struct-like objects in Java can be characterized by:
- No encapsulation: Direct access to fields, just like in a struct.
- No methods (typically): Focusing solely on data storage.
- Immutable or mutable fields based on the requirement.
Implementing Struct-Like Objects Using Classes
A basic approach to mimic structs in Java is by using plain old Java objects (POJOs) or simple classes with public fields.
Example
In the Point class above, fields x and y are public, allowing for easy access without getter or setter methods, akin to a struct.
Pros and Cons
| Pros | Cons |
| Simple and direct implementation | Breaks encapsulation principles |
| Easy to read and maintain for straightforward use-cases | Fields are mutable which might lead to unintended changes |
Using Records for Struct-Like Objects
Records in Java are a more recent addition, primarily designed to model immutable data carriers, making them apt for struct-like objects.
Example
The Point record automatically provides a constructor, field accessors, equals(), hashCode(), and toString() implementations. This concise syntax reduces boilerplate and enhances readability and maintainability.
Pros and Cons
| Pros | Cons |
| Immutability ensures thread safety | Requires Java 16 or newer |
| Less boilerplate compared to POJOs | Limited flexibility in terms of methods |
Comparison of Struct-Like Implementations
| Feature | POJO/Classes | Records |
| Boilerplate | High | Low |
| Immutability Support | Requires extra code | Automatic |
| Readability | Moderate | High |
| Thread Safety | Needs management | Inherent |
| Compatibility | All Java versions | Requires Java 16+ |
Custom Implementations with Interfaces and Builders
For advanced use-cases, combining interfaces and builders can emulate more sophisticated struct-like entities with controlled immutability and customization.
Example
The above approach provides an immutable struct-like interface, implementing encapsulation while allowing factory methods for instantiation.
Conclusions
While Java does not provide native support for struct types, it enables developers to achieve similar patterns using classes, records, and interfaces. Understanding these alternatives will allow developers to make informed choices based on requirements, facilitating efficient data handling while aligning with Java's object-oriented principles.
Additional Notes
- For mutable structures in Java, always ensure synchronization in concurrent environments.
- Java's design choices, focusing on encapsulation, might resist a direct struct-like implementation, encouraging practices that promote data safety and integrity.
- When targeting lower Java versions, consider POJOs or custom interfaces.
Conclusively, crafting struct-like objects in Java requires a careful balance of simplicity, clarity, and adherence to object-oriented design principles.
Related reading

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.