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]

4

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

Now, you have a value "Eggs" that is encompassed in double quotes ". This already is a hint on the type of the variable.

In Java, Strings are encompassed by double quotes.

So, you need to declare a variable, called name, that can hold a String "Eggs".

What you need to do is:

String name;

You need another instance variable cost that can hold a value of 1.21. The decimal point again hints the data type: double.

So, the next instance variable is:

double cost;

Last, you need an instance variable aisleNum that can hold a value such as 5. The missing decimal point hints that you should use an int:

int aisleNum.

Now, you need the constructor that takes the parameters name, cost, aisleNum.

Constructors are methods that have the same name as the class:

public class GroceryItem {
    // here, you would put the instance variables (class fields) from above

    public GroceryItem(String name, double cost, int aisleNum) {
        // that is the constructor
        // here you will need some code
        // that assigns the values of the parameters passed
        // to the instance variables.
    }

    public String getName() {
        // This method should return the name of the 
        // GroceryItem
        // Since the name is a String, you will need to return a String
     }
}

1

u/yodawg32 Sep 06 '15

Doing this.name = name etc. What it specifically do? How does it effect those variables in other parts of the program?

1

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

this.name = name

What it specifically do?

The statement as such does nothing but assign the parameter that was passed in the constructor (or in any method for that matter) to the class-internal variable (field).

This step is necessary so that the class gets it's data that it needs to work with.

Think about a simple Employee class. Each Employee has a name, employeeId, a baseSalary, and more.

In the constructor you give meaningful values to those variables so that later in the program you can calculate the salary based on the baseSalary of the specific employee and the hoursWorked (that can come from a different source, like a spreadsheet or similar).

1

u/yodawg32 Sep 06 '15

Okay so can't any method do this? What is the difference between a constructor and any normal method doing it?

1

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

The constructor performs some additional functions behind the scenes that initialize the object itself.

Constructors are called as soon as you use the new keyword.

You can use "normal" methods after the object is initialized by the constructor.

1

u/yodawg32 Sep 06 '15

So,you're saying the actual (this) variables get turned into Objects? I understand the new keyword and how you pass arguments into it so it matches the constructor. What is the significance of turning those variables into Objects? You can still use the variables fully without initialising them right?

1

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

Yes, you can say that this represents the current instance of the class, i.e. the current object

There can only be one this per class as it refers to the object itself.

You are not turning the variables inside the class into objects. These variables already belong to the object and are properties of the object that describe it.

You can still use the variables fully without initialising them right?

Depends: primitive types (boolean, byte, char, int, short, long, double, float) always are initialized by default (numeric types to 0, boolean to false). Object types (like String) must be initialized otherwise they contain null which will lead to NullPointerExceptions.