0

I'm trying to understand the class and variables system in Java but I think I'm not thinking java yet...I manages to get what I want but I feel it's not in the right way. Can you help me do it good? The best is that I show you a code example:

public class Food {
    static Product tomato = new Product("tomato")

    public static void main(String[] args) {
        Order order = new Order(2);
        System.out.println(order.cost);
    }

    static class Order {
        double cost;

        Order(int nbTomato) {
            cost = tomate.prix * nbTomato;
        }
    }
}

and then in another file

public class Product {
    String nom;
    double prix;

    public Product(String name_,int price_) {
        name = name_;
        price = price_;
    }
}

Ideally, I would like "tomato" (and other such variables: eggplant, ...) to be defined in another file, used by several classes, and they will not be modified further. Still they share some fields (price, name, ...), so they should all derive from a given class.

16
  • You can make a global variable in your class. Just declare the tomato outside of any functions in your class.
    – Arc676
    Commented Jan 23, 2016 at 7:48
  • "but I feel it's not in the right way." And why do you feel that? Btw: "that is accessible by a method called order" This is wrong. You don't have a method called order, you have a constructor called Order. Please mind the difference of method and constructor and note the difference between order and Order (the uppercase "O" counts).
    – Tom
    Commented Jan 23, 2016 at 7:49
  • If you want to access variable in other class type object, then pass reference of the object to the class in which you want to access.
    – null1941
    Commented Jan 23, 2016 at 7:50
  • Your code is good. (but tomato price always changes...)
    – ZhongYu
    Commented Jan 23, 2016 at 7:53
  • @Tom How is a constructor not a method?
    – nicomp
    Commented Jan 23, 2016 at 7:54

2 Answers 2

1

Usually in these situations you have to think like in real life. Imagine your problem as a real life situation and you will understand what to implement and what you need. For e.g.

You need to be able to work with Products. Which means it's alright to have a class Product. But will you ever be able to work directly with a product? Or will you be only working with derivatives of a product (Oranges, Apples, Bananas) ? This means using "abstract class".

Now that you use abstract class, you can never touch an actual product because you can not have an instance of it, but only an instance of a type-of-product. Now you ask yourself: Will I be needing a type of fruit which derives (extends) class Product, and each of them has it's own special characteristic? Or do I not care and consider that Apples and Oranges all are fruit, and differ only by name.

If you want to have an orange and have the ability to call orange.peel() , and have an apple and have the ability to call apple.rott(), then you should create diffrent classes for Apples, Oranges, etc. , each extending product, each having their own methods inside. If not, you can just create a class Fruit, extending product, and each fruit differing from fruit.setName(String name)

Now, you want to be able to Order something. The Order class you created is ok, but you will never know how to differ from one fruit to another. So, instead of creating separate methods "orderOranges(number), orderBananas(number)", you create a generic method that accepts any Product

order.orderProducts (List<Product> products)

Where the method should look like this

public int orderProducts (List<Product> products) {
    int price = 0;
    for (Product prod : products) {
        price += prod.getPrice();
    }
    return price;
}

This method is similar to taking your groceries to the check-out section. Now, you can add anything you want. The quantity format, or whatever you want.

If you want to be able to access your products from anywhere in the program, you should (as in reality) create a static class Basket, which contains a list of products, where you can .add(Product), throwOut(Product), and you can send this basket to the check-out order.checkOut(Basket basket); Everything is similar to how reality works. I hope this is the guidance you wished for.

3
  • Ok, thanks for your answer, so I should define an abstract class "Tomato" that extends "Product"? Should it be public? static? defined inside the class "Product"? What about using an "interface"? Commented Jan 23, 2016 at 8:28
  • The abstract class you want to define is the "Product" class. Because everything you work with (Bananas, Oranges, Apples) are Products , so Product is an abstract type. In the same way, you can use an Interface. In you case it's as good as using an abstract data type.
    – ctotolin
    Commented Jan 23, 2016 at 8:57
  • This is a tutorial which should explain what I said before in other works. I just googled for an example of code and found one with fruits and stuff. Hope it helps link
    – ctotolin
    Commented Jan 23, 2016 at 9:00
1

In your example there are a few things to understand:

  • tomato will be a static variable from your Food class - that means it will be only one tomato for any instance of Food (that there is only one tomato for all instances (they share the same tomato))
  • there is a static class Order inside the Food class

If you really want tomato to be accessible lika a global variable it would be better to make it public declaring it like:

public static Product tomato = new Product("tomato", 42);

It's a good practice to declare constants as final and as a convention in upper case:

public static final Product TOMATO = new Product("tomato", 42);

It will be accessible as a static variable of your food class, just like this:

Product myTomato = Food.TOMATO;

When you say that it should be accessible by a method order you could do something like this (inside anyclass):

public void order() {
    Product myTomato = Food.TOMATO;
    // Your code here
}
1
  • That seems like a good solution, thanks. Ideally, I would like tomato (or TOMATO) to be in a separate file that can be edited without entering the main program. Do you see a solution for that? Commented Jan 23, 2016 at 9:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.