bardockyo bardockyo - 1 year ago 111
C Question

Creating struct like data structure in Java

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?

hqt hqt
Answer Source

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) { = name;
      this.code = code;

       // getter
       public String getName() { return name; }
       public int getCode() { return code; }
       // setter

       public void setName(String 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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download