Creating struct like data structure in Java
I would create a public class with public fields and a default constructor, like this:
public class Employee {
public String name, last_name;
// constructor
public Employee() {
this.name = "";
this.last_name= "";
}
}
....
//when using it
Employee e = new Employee();
e.name ="Joe";
e.last_name = "Doe";
A struct in C just like a class in Java and much more powerful, because class in Java can contain method, and C++ does. You create a new class. For example :
class Employee {
private String name;
private int code;
// constructor
public Employee(String name, int code) {
this.name = name;
this.code = code;
}
// getter
public String getName() { return name; }
public int getCode() { return code; }
// setter
public void setName(String name) { this.name = name; }
public void setCode(int code) { this.code = code; }
}
And when you want to create multi employees, create array just like in C:
Employee[] arr = new Employee[100]; // new stands for create an array object
arr[0] = new Employee("Peter", 100); // new stands for create an employee object
arr[1] = new Employee("Mary", 90);
In Java 16 there are Records
(JDK Enhancement Proposal 395):
public record Employee(String firstName, String lastName, String id) {
}
This comes with a canonical constructor, field accessors, hashCode()
, equals()
and toString()
implementations.