1

I'm having problem understanding "static". I have created class "pizzaMenu" containing ArrayList which contains pizzas. All pizzas are added within that class, and it shouldn't be modified later. But I want to access data about pizzas.

Everything is working properly, but I have to create object of "pizzaMenu" in "main" to make it work.

How can I properly initiate such instances, so I have access to them? But the initiation won't be visible in "main"? I'd like to keep "main" as clean as possible.

Loren
  • 9,783
  • 4
  • 39
  • 49
karjan
  • 936
  • 1
  • 7
  • 17

2 Answers2

2

but I have to create object of "pizzaMenu" in "main" to make it work.

Yes, you need an instance of new PizzaMenu() in order to access that menu's items.

I'm having problem understanding "static".

Nothing in PizzaMenu should really be static (other than main, if you have it there). But, it basically mean it belongs to the class, and not any one instance of that class.

In other words, if you did make the list of pizzas static, then that says that all menus have the same pizzas, which shouldn't be true, right? Different places have different menus.

Here's an example.

public class PizzaMenu {

    private List<String> pizzas = new ArrayList<>();

    public void add(String name) {
        pizzas.add(name);
    }

    public List<String> getPizzas() {
        return pizzas;
    }

    // You can also move this to another class
    public static void main() {
        PizzaMenu menu = new PizzaMenu();
        menu.add("Cheese");

        for (String pizza : menu.getPizzas()) {
            System.out.println(pizza);
        }
    }
}

All pizzas are added within that class, and it shouldn't be modified later.

You can use this, for example

private final List<String> pizzas = Arrays.asList("Cheese", "Pepperoni");

But, again, I think different instances of menus should have different pizzas.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    In your example with `final`, `pizzas` is still mutable. You should use `Collections.unmodifiableList()` to make the List immutable (since it shouldn't be modified later). – Polygnome Apr 08 '17 at 22:32
  • Hmm, I think I'm just looking for problem where there is none. I just wanted to remove: PizzaMenu menu = new PizzaMenu(); from the "main". To keep "main" clear. Thanks for your time. – karjan Apr 08 '17 at 22:36
  • @Polygnome You can't add/remove from `Arrays.asList`, but yes, you can update the elements directly. – OneCricketeer Apr 08 '17 at 22:45
  • @karjan You **can** move `PizzaMenu menu = new PizzaMenu()` outside of main. Then, it becomes a member variable of the outer class, **but**, then it **must** be `static` – OneCricketeer Apr 08 '17 at 22:46
  • Its not about adding/removing. `Arrays.asList()` is only fixed in size with changes written through to the backing array. `pizzas.set(0, "Not a pizza");` is a perfectly valid call that writes through in your example. Thus, to truly fulfil the requirment of "should not be modified later" the list should be wrapped into an immutable read-only wrapper. – Polygnome Apr 08 '17 at 22:56
  • @cricket_007 Yes cricket that was my first idea. But to access PizzaMenu menu = new PizzaMenu() outside of main. I still hava to make an instance of the class containing it, inside main. I'm right? – karjan Apr 08 '17 at 23:02
  • @karjan I don't think so... If you make a `static` instance of a `PizzaMenu` outside of `main`, then you can also use that same instance across other methods of the same class without a new one – OneCricketeer Apr 08 '17 at 23:05
  • 1
    But then, making `PizzaMenu` static is almost certainly wrong. Global state is bad. Why don't you want to declare an instance, exactly, @karjan? I don't see a valid engineering reason in anything you've said so far. – Lew Bloch Apr 08 '17 at 23:22
  • @LewBloch I thought it's unnecessary. I see that I was wrong – karjan Apr 08 '17 at 23:29
0

You can declare pizzas as public static within pizzamenu.

public final static List<pizza> pizzas;

So you can access the pizza using

PizzaMenu.pizzas.

Also you mentioned

I'd like to keep "main" as clean as possible.

I think your concept about clean code is not fully correct.

stinepike
  • 54,068
  • 14
  • 92
  • 112