2

lets say I'm constructing a FoodItem, and I want to enter a new item, I want to make sure that the item's name cannot be changed and so does the price of it (meaning I cannot use Setter to change it) but I will be able to change the quantity.

My question is as follows: by declaring the finals NAME and COST in the constructor I cannot access them out side of it, for example for a method like getName() or getCost(). I hope my question is clear enough, if not just ask and I'll reply.

I'm still a beginner at java so forgive me if I'm missing something obvious, I'm here to learn!

public class FoodItem {

    private int _quantity;

    public FoodItem (String name, int cost, int quantity){   // 
        final String NAME = name;
        final int COST = cost;
        _quantity = quantity;
    }

    public String getName(){ return NAME;} //<-- obviously cannot be found
    public int getCost(){ return _cost;}
    public int getNumber(){ return NUMBER;}
    public void setCost(int newCost){ _cost = newCost;}
Sanket
  • 19,295
  • 10
  • 71
  • 82
Immanuel
  • 172
  • 1
  • 13

2 Answers2

3

NAME and COST are declared in the wrong spot. Move their declaration where _quantity is declared.

private int _quantity;
private final String NAME;
private final int COST;

public FoodItem (String name, int cost, int quantity){ 
    NAME = name;
    COST = cost;
    _quantity = quantity;
}

See answer here.

Community
  • 1
  • 1
zeb
  • 100
  • 9
  • Thank you for the quick reply, I didn't try to declare them at the beginning because I assumed I would not be able to change them (or assign a value to them). Could you help me understand why is this different then using a Setter ( which doesn't work). Is it because the are set to final only after they receive a value? or is it the fact that they were created in the constructor? – Immanuel Jul 17 '16 at 16:33
  • 1
    Finals can only be set at the time of object creation by the constructor. – zeb Jul 17 '16 at 16:38
  • I am not sure that this code will complies because if you declare a variable as final you have assing some value to them and also you can't reassign a new value. – Naga Srinu Kapusetti Jul 17 '16 at 19:15
  • `Finals can only be set at the time of object creation by the constructor` huh, I did not know that. Thanks zeb. More about this: http://stackoverflow.com/questions/11345061/why-must-a-final-variable-be-initialized-before-constructor-completes – Yar Jul 17 '16 at 19:21
1

To answer your question First you should know the Data Hiding concept

you can hide the data by not giving the access (means private scope without access methods)

so in this example you could write as below

public class FoodItem {

    private int quantity;
    private String name;
    private int cost;

    public FoodItem (String name, int cost, int quantity){   // 
        this.name = name;
        this.cost = cost;
        this.quantity = quantity;
    }

    public String getName(){ return name;}
    public int getCost(){ return cost;}
    public int getQuantity(){ return quantity;}
    // providing a setter so that some one can change the quantity 
    public void setQuantity(int quantity){ this.quantity = quantity;}

}

In the above example I haven't provided setters for name and cost so no one can access them and no one can change their values (except when they are created) and for the quantity as you mentioned it can be modified so that's why I have provided the setter, so if some one wants to change the quantity then can make a call to obj.setQuantity(<new quantity>); method

Hope you got the concept.

Yar
  • 7,020
  • 11
  • 49
  • 69
Naga Srinu Kapusetti
  • 1,563
  • 15
  • 12