title: How to declare an ArrayList of objects inside a class in java?
body: <p>I'm very unskilled with java and programming, though I am trying to do one of my friend's home exercises! :P</p>
  
  <p>The exercise consists in creating a system that shows a list of products available in a store, giving the user the ability to choose what he/she wants, asking for confirmation, then adding to a cart for a later checkout.</p>
  
  <p>The problem is, I'm trying to make a class template for the products, then putting them all together in an ArrayList of objects from the class "Product". Works fine if I declare said list and add all the objects I want to it inside a function, but I can't declare it inside a class (as if it is a variable of sorts).</p>
  
  <p>Here's part of the code:</p>
  
  <pre><code>package system;
  
  class Product {
  
  public int id;
  public String name;
  public double price;
  
  public Product(int startId, String startName, double startPrice){
       startId = id;
       startName = name;
       startPrice = price;
   }
  
  public int getId(){
      return id;
  }
  
  public double getPrice(){
      return price;
  }
  public String getName(){
      return name;
  }
  
  
  }
  //Main Class (The one that has the 'main()' function)
  
  public class System {
  
  ArrayList&lt;Product&gt; list = new ArrayList&lt;Product&gt;();
  list.add(new Product(0, "Coca-Cola", 3.00));
  list.add(new Product(1, "Mountain Dew", 2.00));
  list.add(new Product(2, "Mango Smoothie", 4.50));
  list.add(new Product(3,"Orange Juice", 5.00));
  list.add(new Product(4, "Dr. Pepper", 4.50));
  list.add(new Product(5, "Sandwich", 3.00));
  list.add(new Product(6, "Hamburger", 3.50));
  list.add(new Product(7, "Light Sandwich", 4.00));
  
  
  //Yadda Yadda Yadda
  }
  </code></pre>
  
  <p>The purpose of doing this is to be able to access the product object in question by simply inputting its id, which is identical to its position in the ArrayList.</p>
  
  <p>e.g.:</p>
  
  <pre><code>System.out.println(list[0].getName); //Prints "Coca-Cola"
  </code></pre>
  
  <p>This strategy makes it easier to access each product, and modularizes it better, allowing me to add changes in the future.</p>
  
  <p>The only problem is, as I've said, I can't declare it at the beginning of a class. It can only be declared inside a method, and only used in said method, apparently.</p>
  
  <p>Any suggestions on how I may proceed? I tried researching a bit but couldn't find/understand any suitable answers.</p>
  
  <p>Also, any tips on how to make this code neatier is very appreciated.</p>
  
  <p>Thanks!</p>