Java Struct: Examples and When to Use

Java-Struct-feature-image.jpg

When developers familiar with C and C++ learn Java, they often search for Java structs, which in C++ were lightweight data containers used to group related variables. These containers make it easier to organize and manage data, particularly in system-level programming, where memory control and efficiency are important. However, Java does not support a native struct keyword in the same it is done in C/C++. This is because it promotes object-oriented programming design. To achieve struct-like functionality in Java as well developers also use classes and other techniques. In this article, we will discuss all these methods and introduce you to “Java Struct” functionality with examples. 

What is a Struct in Programming?

A struct in programming is a data type that is used by developers to group multiple variables under a single name. Unlike arrays that hold values of the same type, a struct can store variables of different data types, such as integers, floats, and strings. This makes it useful for modeling real-world entities. In fact, before the concepts of object-oriented programming became mainstream, structs were used in place of classes (which are now used in modern programming). 

Struct in programming was used in the following scenarios:

  • To represent simple data models like a Point(x, y) in graphics or 
  • It was also used to store records like student information or employee data.
  • A more complex usage of struct was for handling low-level memory structures in system or embedded programming
Free Online Java Certification Course
This free self-paced course will help you build your fundamentals in Java
quiz-icon

Does Java Have Structs?

Like we mentioned earlier, Java does not provide a native struct keyword like C or C++. This is because Java is actually an object-oriented design-based language,  where data and behavior are encapsulated inside classes and objects. Instead of relying on lightweight data containers like structs, Java actually uses classes, records (introduced in Java 14), and objects to represent structured data. Java Struct is not exactly a thing. 

While this may seem new and restrictive for developers coming from C or C++, the advantage lies in Java’s approach. This OOPS approach ensures consistency (the entity used throughout the code is the same), reusability (reducing boilerplate code and redundancy), and scalability (for large applications). For scenarios that require struct-like behavior, like working with native code (code that is written in a low-level language like C or C++), developers often use an alternative like JNA’s (Java Native Access) Structure class or similar libraries.

Note: An example of using Native code could be if you write a C function for high-performance math operations and want to call it from Java, the compiled C library is native code.

How to Simulate a Struct in Java

Although the Java struct keyword is not in the default library, those objectives can be achieved in multiple ways, depending on whether you need a lightweight structure for data storing purposes, you want immutability, or you want the structure to be compatible with a native library. Let’s look at the major possibilities.

1. Using Classes

In Java, the most widely used method to mimic struct behaviour is to create a class that contains only fields and fewer methods. This allows developers to group related variables into a single unit, much like a struct in C.

In the example below, the Student class has three fields. You can create an object of the Student class and assign values, as follows:

Code:

Java

Output:

Using Classes in Java to mimic Java Struct

Explanation: 

Using classes to replicate Java structs is a better approach for beginner programmers and for small data containers when immutability does not matter. But classes can become more swollen than structs in C or C++ because classes support not only inheritance, encapsulation, and other OOP features.

Get 100% Hike!

Master Most in Demand Skills Now!

2. Using Records (Java 14+)

Another way to achieve struct-like behaviour is through records, which were introduced in Java 14 to reduce boilerplate code and serve as immutable data carriers. Records are closer to structs in spirit because they allow developers to store grouped values with less effort.

In this example, the Student record will automatically create the constructor, equals(), hashCode(), and toString() methods for us.

Code:

Java

Output:

Java Records for Java Struct

Explanation: 

Records are the optimal choice when developers are looking for lightweight, read-only data structures. The only issue with records being immutable is that developers cannot update their values after they have been assigned.

Java Struct-Like Behavior with Libraries

When developers need to deal with native code (such as C or C++ libraries or system APIs), developers have to rely on the Java Native Access (JNA) library. JNA provides a Structure class, which allows the JNA code to work more like a struct in C by mapping Java fields directly to memory layouts.

Code:

This is the Student.java file that holds the Java struct of the Student class. To use this Java struct, you will need to make a Main.java file that will have the main class.

Student.java

Java

Main.java

Java

Output:

Java Struct using JNA Library

Explanation: 

Using JNA’s Structure makes the most sense when developers are working in system-level programming, calling native APIs, or are otherwise dealing with applications that are sensitive to performance. Although using a Structure creates a dependency on external libraries, that may not be good for simpler use cases.

When to Use Java Structs vs Java Classes

Since Java does not have native structs, developers often wonder when to use alternatives such as classes, records, or struct-like libraries. The decision usually depends on factors like memory efficiency, mutability, and best practices in application design. The table below highlights the key differences and scenarios where each option makes sense.

Aspect Struct-Like (Records / JNA) Classes in Java
Memory Efficiency Struct-like data containers are more lightweight and can be mapped efficiently in memory (e.g., records, JNA). Classes are heavier because they carry object metadata and additional OOP features.
Mutability Records are immutable by design, while JNA structures mimic C-style structs but can be mutable. Classes can be either mutable or immutable, depending on how they are written.
Best Practices Struct-like approaches are best for small, data-centric models or when working with native code. Classes are best for general-purpose design, encapsulation, and when behavior and data should be grouped together.

Real-World Examples of Java Struct-Like Usage

  • It is common for developers to use Java records to represent simple and immutable data model objects, like storing a student or entity, or product representation, in as few lines as possible. 
  • JNA structures are a common way to handle the fact that Java will often make system calls to native C or C++ libraries. 
  • In game development and other fields, developers often employ struct-like (classes) to represent positions, coordinates, shapes, and other similar and efficient calculations, but without adding the extra complexity that object-oriented analysis adds. 
  • Struct-like containers are also decent representations of embedded systems when memory is important, and it is more efficient to group variables together and simplify computations.

Common Mistakes Developers Make When Using Java Struct

  • Many developers incorrectly think that Java supports structs directly, which causes confusion, incorrect syntax, and often compilation errors to stem from transitioning from C or C++. 
  • Some developers misuse Java classes as structs and ignore object-oriented principles of encapsulation, thus inadvertently also creating logic that is fragile and becomes difficult to maintain. 
  • Developers often forget that Java records are immutable and attempt to change field values after the record has been created, which results in errors and misuse of the application’s design. 
  • If developers outside their domains of responsibility use external libraries such as JNA to perform work that could instead be done with simple Java classes or records, that again causes unnecessary complexity and performance overhead to result in Java projects.
Full Stack Development Course for Undergraduates
Join our Full Stack Developer course, taught by industry experts.
quiz-icon

Conclusion

Although Java does not have a native struct keyword like in C or C++, it instead has a number of different approaches that will achieve similar goals depending on the use case. A developer can use classes for more flexible, mutable data models, Java records if all they want is a concise and immutable data container, or turn to a library like JNA when they want to interact with the native language directly. Each of these alternatives of Java Struct has its own benefits and disadvantages, but in total, they allow Java to maintain its object-oriented design while also offering some flexibility in how we can represent structured data. Understanding when to use classes, records, or a struct-like library is a step towards cleaning the code, being efficient, and, most importantly, maintaining the code going forward.

Useful Resources:

Java Struct – FAQs

Q1. Is there a struct in Java?

Java does not have a native struct like C or C++. Instead, Java uses classes, records, or external libraries to achieve similar functionality.

Q2. What is the concept of struct in Java?

The concept of struct in Java is implemented using classes or records. These allow developers to group multiple fields into a single unit, similar to C/C++ structs.

Q3. Is struct faster than class in Java?

Structs do not exist in Java, but in C/C++, they can be faster due to direct memory allocation. In Java, performance depends on how classes or records are used.

Q4. What are the six structures in Java?

The six common data structures in Java are arrays, linked lists, stacks, queues, hash maps, and trees. These are widely used for efficient data organization and manipulation.

About the Author

Software Developer | Technical Research Analyst Lead | Full Stack & Cloud Systems

Ayaan Alam is a skilled Software Developer and Technical Research Analyst Lead with 2 years of professional experience in Java, Python, and C++. With expertise in full-stack development, system design, and cloud computing, he consistently delivers high-quality, scalable solutions. Known for producing accurate and insightful technical content, Ayaan contributes valuable knowledge to the developer community.

Full Stack Developer Course Banner