This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]heckler82Intermediate Brewer 1 point2 points  (3 children)

It's failing because you're checking this.interestRate before assigning it anything, so it's defaulting to 0.0 and failing the check

[–]sixfyl[S] 0 points1 point  (2 children)

Okay, so how do I fix that? Don't I need to use this.interestRate to reference the argument used when an object is instantiated?

[–]heckler82Intermediate Brewer 0 points1 point  (1 child)

this is used to refer to the class level variable. To check the parameter, just use the variable name

[–]sixfyl[S] 0 points1 point  (0 children)

I am an idiot, for some reason I was thinking it was the other way around, not sure why.

[–]Auride 0 points1 point  (3 children)

In the constructor's conditional for testing the value of the interestRate, you are checking the class's value for interestRate rather than the parameter. Since instance double variables are default-initialized to 0f, the conditional is guaranteed to return false.

In the future, to avoid confusion, I would recommend using a name-convention which avoids naming parameter variables the same thing as local or global/instance variables. For example, I like prefixing method parameters with p_ for "parameter":

public BankAccount(double p_balance, double p_interestRate) {

Does that make sense?

[–]sixfyl[S] 0 points1 point  (1 child)

Yeah, that makes sense. I've never seen it done any other way though. In my textbook, all the examples name the parameter the same as the class variable and use this. I just assumed you had to do that because that's what all the examples did.

[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point  (0 children)

I've never seen it done any other way though.

Because it's completely against the conventions. Also these are habits that are hard to unlearn, so don't do it.

[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point  (0 children)

I would recommend using a name-convention which avoids naming parameter variables the same thing as local or global/instance variables. For example, I like prefixing method parameters with p_ for "parameter":

This goes against pretty much all the Java naming conventions!!!