views:

310

answers:

3

I have a class named Storage. Storage contains an arraylist of special objects called Products. Each product contains information such as name, price, etc. My code is as follows:

class Storage{

 Product sprite = new Product("sprite",1.25,30);
 Product pepsi = new Product("pepsi",1.85,45);
 Product orange = new Product("orange",2.25,36);
 Product hershey = new Product("hershey",1.50,33);
 Product brownie = new Product("brownie",2.30,41);
 Product apple = new Product("apple",2.00,15);
 Product crackers = new Product("peanut",3.90,68);
 Product trailmix = new Product("trailmix",1.90,45);
 Product icecream = new Product("icecream",1.65,28);
 Product doughnut = new Product("doughnut",2.75,18);
 Product banana = new Product("banana",1.25,32);
 Product coffee = new Product("coffee",1.30,40);
 Product chips = new Product("chips",1.70,35);

 ArrayList<Product> arl = new ArrayList<Product>();

 //add initial elements to arraylist
 arl.add(sprite);
 arl.add(pepsi);
 arl.add(orange);
 arl.add(hershey);
 arl.add(brownie);
 arl.add(apple);
 arl.add(peanut);
 arl.add(trailmix);
 arl.add(icecream);
 arl.add(doughnut);
 arl.add(banana);
 arl.add(coffee);
 arl.add(chips);
}

Whenever I compile, I get an error message on lines 141-153 stating <identifier> expected. I know it's an elementary problem, but I can't seem to figure this out. Any help is much appreciated.

+3  A: 

You can't call methods like that just in the class body. You have to put methods calls in other methods, or in a constructor.

You want this:

class Storage{

    Product sprite = new Product("sprite",1.25,30);
    Product pepsi = new Product("pepsi",1.85,45);
    Product orange = new Product("orange",2.25,36);
    Product hershey = new Product("hershey",1.50,33);
    Product brownie = new Product("brownie",2.30,41);
    Product apple = new Product("apple",2.00,15);
    Product crackers = new Product("peanut",3.90,68);
    Product trailmix = new Product("trailmix",1.90,45);
    Product icecream = new Product("icecream",1.65,28);
    Product doughnut = new Product("doughnut",2.75,18);
    Product banana = new Product("banana",1.25,32);
    Product coffee = new Product("coffee",1.30,40);
    Product chips = new Product("chips",1.70,35);

    ArrayList<Product> arl = new ArrayList<Product>();


    //constructor
    protected Storage(){
        //add initial elements to arraylist
        arl.add(sprite);
        arl.add(pepsi);
        arl.add(orange);
        arl.add(hershey);
        arl.add(brownie);
        arl.add(apple);
        arl.add(peanut);
        arl.add(trailmix);
        arl.add(icecream);
        arl.add(doughnut);
        arl.add(banana);
        arl.add(coffee);
        arl.add(chips);
    }
}
Cam
I believe that may have solved it, thanks a ton!
A-moc
Sure. You might want to try polygenelubricants's answer too - it's a bit less repetitive.
Cam
+2  A: 

The problem is that your initialization code is out of place. You can:

  • Put it in a constructor or other method
  • Put it in instance initializer
  • Put it as field initializer

The last option is the simplest and cleanest solution; it would look something like this:

List<Product> arl = new ArrayList<Product>(
  Arrays.asList(
    sprite, pepsi, orange, hershey, brownnie, apple, peanut,
    trailmix, icecream, doughnut, banana, coffee, chips
  )
);

Note also that I switched the type of arl to its interface List<Product>. You should try to work with interfaces rather than concrete implementations whenever possible.

polygenelubricants
Why would you want to work with the interface? How is that better than working directly with ArrayList? +1 for the Arrays.asList instanstiation though.
Cam
Among other things, because: (i) more flexibility; you don't depend on one particular implementation (ii) better encapsulation; you're restricted to using only what the interface provides.
polygenelubricants
I think for some situations you're right with regards to flexibility. For example that way you can easily swap out different implementations for testing purposes. But for some other situations, you might want to take advantage of certain functionalities provided by the implementing class (ArrayList or whatever the case may be) on top of the methods defined in the interface - no? And what's to say List provides better encapsulation than ArrayList?
Cam
+1  A: 

Your class is missing some methods. Use a constructor or a main method (public static void main(String[] args){...}) to fill your ArrayList.

James P.