. Advertisement .
..3..
. Advertisement .
..4..
If you are coming from C-style languages, structures are an essential part. But they aren’t supported everywhere. Read on to find out how to implement struct in Java.
Struct In Java
Struct In Programming Languages
In many programming languages like C, C++, and C#, struct is a complex data type that supports encapsulation of related data. Typically, this composite data structure is used to create mixed-data-type records, including multiple variables and methods.
In C, these variables are placed under one contiguous block of memory, enabling access to different variables through just one pointer. A struct in C++ is almost identical to a class. The primary difference comes from the default visibility. While struct members are public, the members of a class are private by default.
C#’s struct data type is also similar to the class type. But unless you use pass-by-reference, a struct is always passed to a function as an argument. Unlike classes, modifications made by the function will not reflect in the original struct.
Java’s Support Of Struct
While struct is a popular feature, there is no equivalent to it in Java. This programming language is created to remove redundancy from C/C++, including complex data types like structures and unions.
The official rationale of Java’s creators is that you don’t need these data types anymore since everything can be achieved with classes with instance variables. They argue that you can declare classes and then make instance variables public or private, depending on your needs.
Classes are the best suggestion you can have if you want to implement structs in Java. It is a neat way to bundle methods and data, keeping some of them private.
Keep in mind that structures aren’t without flaws. They can be prone to abuse of public, mutable members. However, if you deem your use-case safe, structs can be a useful tool too.
This is an example of how to use classes to make a struct-equivalent data type in Java:
class Site {
String name;
String URL;
int ranking;
String top_language;
public Site(String a_name, String a_URL, int a_ranking, String a_language){
this.name = a_name;
this.URL = a_URL;
this.ranking = a_ranking;
this.top_language = a_language;
};
}
public class MyClass {
public static void main(String[] args){
Site ittutoria = new Site("ITTutoria", "https://ittutoria.net", 1, "Java");
System.out.println(ittutoria.name);
System.out.println(ittutoria.URL);
System.out.println(ittutoria.ranking);
System.out.println(ittutoria.top_language);
}
}
Output:
ITTutoria
https://ittutoria.net
1
Java
The snippet above creates a public class called Site. It has several members that are variables that contain data about a programming website. In the main()
function, we can use the new keyword to initialize an instance of this class, giving it a name and a list of members. You can access members of this instance later, just like how you can access a struct in C or C++.
Project JUnion
You can also use third-party libraries like Project JUnion to use struct-like data types in Java. In particular, JUnion is designed to deliver struct operations with lower memory consumption. You can add the @Struct
annotation before a class declaration to create a struct type:
import theleo.jstruct.Struct;
@Struct
class Site {
String name;
String URL;
int ranking;
String top_language;
Conclusion
There is no official support of struct in Java, but this doesn’t mean you can’t achieve similar tasks. If you want to bundle data and methods together, Java classes are a great choice too. They even allow you to call methods from another class, which you can learn more about in this guide.
Leave a comment