0

It could be a very simple solution but I just started learning Java. I would like to add every instantiated Product to the productList. Is there any way to solve this problem without modifying the access modifiers?

public class Product {
    private int id;
    private String name;
    private float defaultPrice;
    private Currency defaultCurrency;
    private Supplier supplier;
    private static List<Product> productList;
    private ProductCategory productCategory;

    public Product(float defaultPrice, Currency defaultCurrency, String name) {
        this.id = IdGenerator.createID();
        this.defaultPrice = defaultPrice;
        this.defaultCurrency = defaultCurrency;
        this.name = name;
    }
}
4
  • 2
    Why do you have a Product containing a list of Products as a variable on class level (static)? Commented Sep 7, 2018 at 13:42
  • 1
    From a design point of view using static lists in the way you do is normally something you shouldn't do but I'll leave that for now. If productList is initialized (if not you could add =new LinkedList<>() to the declaration) you could just do productList.add(this) in the constructor. Commented Sep 7, 2018 at 13:42
  • 1
    Just delete the static list. it's a bad idea in the real world and doesn't teach you anything either. Commented Sep 7, 2018 at 13:44
  • 1
    If you are modeling something like a manufacturing company, then you would need something like an Inventory class. In that class you would have a list of Product. Each Product would not have list of nested Product. Commented Sep 7, 2018 at 13:47

4 Answers 4

2

You can just add a newly created Product to the list in its constructor:

public class Product {

    private int id;
    private String name;
    private float defaultPrice;
    private Currency defaultCurrency;
    private Supplier supplier;
    private static List<Product> productList = new LinkedList<>();
    private ProductCategory productCategory;

    public Product(float defaultPrice, Currency defaultCurrency, String name){
        this.id = IdGenerator.createID();
        this.defaultPrice = defaultPrice;
        this.defaultCurrency = defaultCurrency;
        this.name = name;
        productList.add(this);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

While this works, having a mutable static collection should be avoided wherever possible +1
1

Change the initialisation line

private static List<Product> productList;

to

private static List<Product> productList = new LinkedList<>();

Add productList.add(this) as the last line of the constructor.

So each time you call the Product constructor, it will add this instance to the static list.

Comments

1

Like Peter Lawrey mentionned it in the comment section of Mureinik's answer, having a static collection in the POJO is not the best solution.

I would suggest to use a simple facade. This limit the list existence to the facade life and don't include the logic of a collection in a POJO.

public class FacadeProduct {

    private List<Product> cacheProduct = new ArrayList<>();

    public Product createProduct(float defaultPrice, Currency defaultCurrency, String name){
        Product p = new Product(defaultPrice, defaultCurrency, name);
        cacheProduct.add(p);
        return p;
    }
}

This would be quite simple to use.

public static void main(String ars[]){
    {
        FacadeProduct f = new FacadeProduct();
        {
            Product p1 = f.createProduct(1f, null, "test1");
            Product p2 = f.createProduct(1f, null, "test2");
            Product p3 = f.createProduct(1f, null, "test3");
            // Here, the list have 3 instances in it
        }
        // We lose the p1, p2, p3 reference, but the list is still holding them with f.
    }
    //Here, we lose the f reference, the instances are all susceptible to be collected by the GC. Cleaning the memory
}

Comments

0

Initialize productList with null, and then modify the constructor as follows:

public Product(float defaultPrice, Currency defaultCurrency, String name) {
        this.id = IdGenerator.createID();
        this.defaultPrice = defaultPrice;
        this.defaultCurrency = defaultCurrency;
        this.name = name;
        if (productList == null) productList = new ArrayList<>();
        productList.add(this);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.