Does Java support structs?

Dmitry picture Dmitry · Mar 2, 2011 · Viewed 243.6k times · Source

Does Java have an analog of a C++ struct:

struct Member {
  string FirstName; 
  string LastName;  
  int BirthYear; 
};

I need to use my own data type.

Answer

Tom Quarendon picture Tom Quarendon · Mar 2, 2011

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.