r/javahelp • • Sep 06 '15

I need help

For my assignment I need to do this....DECLARE AN INSTANCE VARIABLE CALLED NAME THAT CAN CONTAIN A VALUE SUCH AS "EGGS"....I have no idea where to start. If someone could help me get started hopefully I can finish the assignment. Thanks everyone

0 Upvotes

16 comments sorted by

View all comments

0

u/[deleted] Sep 06 '15

[deleted]

2

u/desrtfx Out of Coffee error - System halted Sep 06 '15

Your Constructor is also wrong:

//Constructors
public GroceryItem(String name, double cost, int aisle) 
{
    name = name;
    cost = cost;
    aisle = aisle;

}

First, you are not using the proper variable name for aisleNum and seconds, which is much worse than the first one, you are assigning the fields wrong:

    name = name;

Actually does nothing.

You need to use this to assign the value to the field.

this.name = name;

And similarly for the rest of the variables.

Had you made your constructor parameters final, your Constructor wouldn't even have compiled.

Good practice:

//Constructors
public GroceryItem(final String name, final double cost, final int aisleNum)

The final keyword prevents accidental manipulation of the parameters.

1

u/Seder777 Sep 07 '15

Thank you so much for your help, sorry I am a rookie at this. I really appreciate it.