Does Java support structs?
The equivalent in Java to a struct would be
class Member
{
public String FirstName;
public String LastName;
public int BirthYear;
};
and there's nothing wrong with that in the right circumstances. Much the same as in C++ really in terms of when do you use struct verses when do you use a class with encapsulated data.
Java definitively has no structs :) But what you describe here looks like a JavaBean kind of class.
Java 14 has added support for Records, which are structured data types that are very easy to build.
You can declare a Java record like this:
public record AuditInfo(
LocalDateTime createdOn,
String createdBy,
LocalDateTime updatedOn,
String updatedBy
) {}
public record PostInfo(
Long id,
String title,
AuditInfo auditInfo
) {}
And, the Java compiler will generate the following Java class associated to the AuditInfo
Record:
public final class PostInfo
extends java.lang.Record {
private final java.lang.Long id;
private final java.lang.String title;
private final AuditInfo auditInfo;
public PostInfo(
java.lang.Long id,
java.lang.String title,
AuditInfo auditInfo) {
/* compiled code */
}
public java.lang.String toString() { /* compiled code */ }
public final int hashCode() { /* compiled code */ }
public final boolean equals(java.lang.Object o) { /* compiled code */ }
public java.lang.Long id() { /* compiled code */ }
public java.lang.String title() { /* compiled code */ }
public AuditInfo auditInfo() { /* compiled code */ }
}
public final class AuditInfo
extends java.lang.Record {
private final java.time.LocalDateTime createdOn;
private final java.lang.String createdBy;
private final java.time.LocalDateTime updatedOn;
private final java.lang.String updatedBy;
public AuditInfo(
java.time.LocalDateTime createdOn,
java.lang.String createdBy,
java.time.LocalDateTime updatedOn,
java.lang.String updatedBy) {
/* compiled code */
}
public java.lang.String toString() { /* compiled code */ }
public final int hashCode() { /* compiled code */ }
public final boolean equals(java.lang.Object o) { /* compiled code */ }
public java.time.LocalDateTime createdOn() { /* compiled code */ }
public java.lang.String createdBy() { /* compiled code */ }
public java.time.LocalDateTime updatedOn() { /* compiled code */ }
public java.lang.String updatedBy() { /* compiled code */ }
}
Notice that the constructor, accessor methods, as well as equals
, hashCode
, and toString
are created for you, so it's very convenient to use Java Records.
A Java Record can be created like any other Java object:
PostInfo postInfo = new PostInfo(
1L,
"High-Performance Java Persistence",
new AuditInfo(
LocalDateTime.of(2016, 11, 2, 12, 0, 0),
"Vlad Mihalcea",
LocalDateTime.now(),
"Vlad Mihalcea"
)
);
Actually a struct in C++ is a class (e.g. you can define methods there, it can be extended, it works exactly like a class), the only difference is that the default access modfiers are set to public (for classes they are set to private by default).
This is really the only difference in C++, many people don't know that. ; )
Java doesn't have an analog to C++'s structs, but you can use classes with all public members.