-2

In java, I'm trying to make a private field for an ArrayList with objects in it, but I'm unsure how exactly this is done. Is this the correct way to do it, or is there another way?

public class EllipsoidList {
   private String name = " ";
   private ArrayList<Ellipsoid> objList = new ArrayList<Ellipsoid>();

} //+++ END OF CLASS +++
BKFG50
  • 1
  • 1
  • This may help: https://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line – Melvin Feb 14 '21 at 23:59
  • Does this answer your question? [Initialization of an ArrayList in one line](https://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line) – tgdavies Feb 15 '21 at 00:01
  • Why wouldn't this be fine? – NomadMaker Feb 15 '21 at 00:03
  • You would need a constructor for you class that sets the name, or a setter for the name. Also, to be useful, a getter. – NomadMaker Feb 15 '21 at 03:53

1 Answers1

1

If you want to realize a List class which contains only Ellipsoid elements, plus some other fields or methods, I suggest the following way:

public class EllipsoidList extends ArrayList<Ellipsoid> {
    String name;
    ...
}

In this way, you inherits all the methods from ArrayList, taking advantages from them, plus you can add more features with your own private fields and methods.

Marco Tizzano
  • 1,611
  • 1
  • 10
  • 18