I am new to Java and I am trying to find out a way to store information like a struct in C. Say for example I want to have a program hire employees. It would take from the user a first name, last name, and id number and would store it. The user could then view that information based off a condition (if the database had more than 1 employee for example). Can any suggest the best way for doing this?
-
2An `object` from a `class` holds all the data you want to pack together. And the methods that manage that data, too. – SJuan76 Feb 10 '13 at 02:51
4 Answers
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);

- 29,632
- 51
- 171
- 250
-
If I create a new class for employee would I want to create array in the employee class or could I created it in a separate class? – bardockyo Feb 10 '13 at 04:00
-
@bardockyo you will create it in separate class. often, array of employee in class such as "Management". – hqt Feb 10 '13 at 10:24
-
1I found a little mistake in setCode(). It should be "int Code" instead of "String code". I want to edit but "Edits must be at least 6 characters" – Pika Sep 01 '16 at 02:23
-
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.

- 6,853
- 5
- 55
- 76
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";

- 541
- 6
- 19
I have a big problem with structures not being supported in Java. I am writing an Android app which receives data via Bluetooth from an embedded ARM based device (written in "C"). This data when received is a packed array of bytes which contains the contents of my embedded device's EEPROM.
In "C", C++, or "C#", I can simply "cast" this stream of bytes to a structure representing the EEPROM on the embedded device and programmatically use the members of the structure simply by referring to them. In Java, I don't have this option and there's no good way to do it since Java (except for an obscure IBM version which supports packed classes) doesn't allow you to have "packed" classes where the class members are packed without headers and footers to each class member.

- 1
-
1In java one there is DataInputStream and DataOutputStream. Or ByteBuffer which is more like struct. – Joop Eggen Jul 01 '19 at 06:45
-
This look like to something unsafe as depending of your compiler, os or processor the padding and optimization done as how the structure in represented in memory would not longer hold true. – Nicolas Bousquet Jan 29 '21 at 15:28